Skip to content

Instantly share code, notes, and snippets.

@alegut
Created February 21, 2020 07:31
Show Gist options
  • Save alegut/5df09dfd9cc687f33267ae1de6202bcc to your computer and use it in GitHub Desktop.
Save alegut/5df09dfd9cc687f33267ae1de6202bcc to your computer and use it in GitHub Desktop.
Find the count of the specified key in array
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