Skip to content

Instantly share code, notes, and snippets.

@JoaoCnh JoaoCnh/obj.js

Last active Oct 14, 2018
Embed
What would you like to do?
Reduce object from array
var obj = songs.reduce(function (acc, currValue) {
var artist = currValue.artist;
var artistCount = acc[artist] ? acc[artist] + 1 : 1;
var newObj = {};
newObj[artist] = artistCount;
return Object.assign(acc, newObj);
}, {});
// ES6
const obj = songs.reduce((acc, currvalue) => {
const artist = currValue.artist;
const artistCount = acc[artist] ? acc[artist] + 1 : 1;
return {
...acc,
[artist]: artistCount,
};
}, {});
console.log(obj); // {Mastodon: 2, Gojira: 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.