DEV Community

Discussion on: Returning an object using reduce()

Collapse
 
craigmc08 profile image
Craig McIlwrath

Seems I didn't describe my idea very well. Here's the code I had in mind:

function getSongCountByArtist(songs) {
  return songs.reduce(function (acc, song) {
    if (acc[song.artist] === undefined) acc[song.artist] = 0;
    acc[song.artist]++;
    return acc;
  }, {});
} 
Thread Thread
 
pjmantoss profile image
PJ Mantoss

Hello Craig! I've implemented your code and it works perfectly now. Thanks a million! I appreciate your help.