Hi good people! I need help with some JavaScript challenge:
QUESTION: Write a function called getSongCountByArtist which takes in an array of song...
For further actions, you may consider blocking this person and/or reporting abuse
You're code is very close, but the function given to reduce should return an object. Instead of returning
acc[val.artist] + songNo
, you could incrementacc[val.artist]
bysongNo
and the returnacc
.Although I'm not sure why you're adding the length of the song name. Maybe just increment by 1 each time?
Hi Craig! I have applied your suggestion. It's still not returning the right data. It returns an empty object. Please See below:
function getSongCountByArtist(arr){
}
//test
getSongCountByArtist(songs); // {}
Thank you
Seems I didn't describe my idea very well. Here's the code I had in mind:
Hello Craig! I've implemented your code and it works perfectly now. Thanks a million! I appreciate your help.
Do not use reduce.
Try :
Array.from(new Set(songs.map(e => e.artist).sort()).values()).map(e => {let obj = {}; obj[e] = songs.filter(f => f.artist == e).length; return obj;})