Created
March 31, 2020 04:40
-
-
Save phuoctamm/cbc7e7c7464ca2c67bee76998d156167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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