Is there a way to turn this…
[{ "name": "Example Playlist", "playlist": ["All", "Listing", "Test"] },{ "name": "Another Example Playlist", "playlist": ["AA", "eee"] }]
To this?
[{ "name": "Example Playlist", "playlist-all": [{"name": "All"}, {"name": "Listing"}, {"name": "Test"}] },{ "name": "Another Example Playlist", "playlist-all": [{"name": "AA"},{"name": "eee"}] }]
I tried to make it by myself with using dozens of forEach but I am really stuck at this point.
Answer
Using Array#map
:
const data = [ { "name": "Example Playlist", "playlist": ["All", "Listing", "Test"] }, { "name": "Another Example Playlist", "playlist": ["AA", "eee"] } ]; const res = data.map(({ name, playlist=[] }) => ({ name, 'playlist-all': playlist.map( e => ({ name: e }) ) }) ); console.log(res);