JS : Convert Array of Strings to Array of Objects

You can use .map() for this. It passes the index into the callback.

myArray = myArray.map((str, index) => ({ value: str, id: index + 1 }));

Leave a Comment