Skip to content

Instantly share code, notes, and snippets.

@phuoctamm
Created March 31, 2020 04:40
Show Gist options
  • Save phuoctamm/cbc7e7c7464ca2c67bee76998d156167 to your computer and use it in GitHub Desktop.
Save phuoctamm/cbc7e7c7464ca2c67bee76998d156167 to your computer and use it in GitHub Desktop.
const array = [
{
id: 1,
name: 'Monkey'
},
{
id: 2,
name: 'Lion'
},
{
id: 3,
name: 'Monkey'
},
{
id: 4,
name: 'Lion'
},
{
id: 4,
name: 'Cat'
}
];
const countArray = array.reduce((prev, cur) => {
prev[cur.name] = prev[cur.name] ? ++prev[cur.name] : 1;
return prev;
}, {});
console.log(countArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment