Created
February 21, 2020 07:31
-
-
Save alegut/5df09dfd9cc687f33267ae1de6202bcc to your computer and use it in GitHub Desktop.
Find the count of the specified key in array
This file contains 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 employees = [ | |
{ id: 101, name: 'john', group: 'tech-lead' }, | |
{ id: 102, name: 'sam', group: 'associate' }, | |
{ id: 103, name: 'danny', group: 'senior associate' }, | |
{ id: 104, name: 'ketty', group: 'senior associate' }, | |
{ id: 105, name: 'mili', group: 'junior' }, | |
{ id: 106, name: 'mouli', group: 'senior associate' }, | |
{ id: 107, name: 'George', group: 'senior associate' } | |
]; | |
const employeesByGroup = employees.reduce((acc, employee) => { | |
acc[employee.group] = acc[employee.group] + 1 || 1; | |
return acc; | |
}, {}); | |
console.log(employeesByGroup); // tech-lead: 1, associate: 1, senior associate: 4, junior: 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment