I feel like this is an easy solve, but I’ve been stuck for a while, so thought I’d reach out for help.
I have an array of objects as per below.
songs = [
{ track_name: "name of track" , track_genres: ["chamber pop","indie pop"] },
{ track_name: "name of track 2" , track_genres: ["dutch r&b","indie soul", "indie pop"] },
{ track_name: "name of track 3" , track_genres: ["indie pop","chamber pop","indie soul"] }
]
I will then make a "mapping" object so each child gets a parent genre assigned to them. I shall be doing this manually (if you have ideas on doing it automatically through code, that’d be great too, but not the core issue for the question)
genres = [
{
name: "Pop",
children: [{name:"Chamber Pop"},{name:"Indie Pop"}]
},
{
name: "R&B",
children: [{name:"Dutch R&B"}]
},
{
name: "Soul",
children: [{name:"Indie Soul"}]
}
]
I now want to run through all tracks in "songs" and populate the size in the respective children of genres. Ideal Output should be:
genres = [
{
name: "Pop",
children: [{name:"Chamber Pop", size:2},{name:"Indie Pop", size:3}]
},
{
name: "R&B",
children: [{name:"Dutch R&B", size:1}]
},
{
name: "Soul",
children: [{name:"Indie Soul", size:2}]
}
]
Any help on this would be appreciated. I’ll be using javascript for this, happy to use lodash/underscore as well if it’s more efficient.
I know how to loop through tracks, but once I have a particular child genre, I’m not able to efficiently map it to the genres variable
2
Answers
Step 1: make a function to count the number of song on each track genre
Step 2: add the result to each
name
inchildren
You can use
forEach()
to iterate over thegenres
and eachchildren
array. Now you can find out how often the genre is used byfilter
ing thesongs
array by genre. Note that you have mixed casing in thesongs
andgenres
array. I solved it by usingtoLowerCase()
, but you may want to fix your data instead.Note that this solution mutates the
genres
array. If you want to keep thegenres
array as-is and generate a new array, you should usemap()
instead offorEach()
and return the modified data.