Let's say I have these nested arrays of albums in an (simplified) array of artists:
const api = [
{
"id": 1,
"name": "John Coltrane",
"albums": [
{ "title": "Giant Steps" },
{ "title": "A Love Supreme"},
{ "title": "Tenor Madness"}
],
"instrument": "Saxophone"
},
{
"id": 2,
"name": "Sonny Rollins",
"albums": [
{ "title": "Saxophone Colossus" },
{ "title": "The Bridge"},
{ "title": "Tenor Madness"}
],
"instrument": "Saxophone"
}
];
I want to combine all the albums into a new array, without any duplicates.
This is what I got so far:
let albums = [];
api.forEach((api) => {
Object.keys(api).forEach((prop) => {
if (prop === 'albums') {
api[prop].forEach(album => albums.includes(album.title) ? '' : albums.push(album.title));
}
});
});
I'm wondering if there's a better, simpler, way to achieve this, and/or if this is going to be slow on a data set that includes about 5000 artists, with 5-20 albums each.
This answer was pointed out to me (possible duplicate), and it does address part of my question (and is very interesting, thanks!), but I'm more interested in how to deal with the nested nature of the array.
Thanks!