How to countBy in JavaScript

 by Robin Wieruch
 - Edit this Post

The countBy function is one of the functions why people use Lodash in their JavaScript code base. Here I want to give you a brief example on how to implement countBy in vanilla JavaScript without Lodash by just using JavaScript's reduce method.

Let's say we have the following and we want to count them by property (here color) to get the following output:

const users = [
{ name: 'Jim', color: 'blue' },
{ name: 'Sam', color: 'blue' },
{ name: 'Eddie', color: 'green' },
];
const countByColor = // TODO: implement countBy
console.log(countByColor);
// { blue: 2, green: 1 }

We can use JavaScript's reduce method on an array to iterate over every item:

const countByColor = users.reduce((acc, value) => {
// TODO: implement countBy
return acc;
}, {});

We start with an empty object as our accumulator (here acc) for this reduce's . For every iteration of the function, we return the changed (here still unchanged) accumulator. Let's implement countBy:

const usersByColor = users.reduce((acc, value) => {
if (!acc[value.color]) {
acc[value.color] = 1;
} else {
acc[value.color]++;
}
return acc;
}, {});

If the accumulator has no count initialized for currently iterated value's color, we initialize it with the count of 1 allocated in the object whereas the color is the key. If there is a count, we can just increment it by one with the ++ operator.

Essentially we start with an empty object and for every iterated value, we negotiate whether we need to allocate a new count with 1 based on the property (here color) in this object. If not, we increment the count by one, because we already started counting it.

Keep reading about 

The groupBy function is one of the functions why people use Lodash in their JavaScript code base. Here I want to give you a brief example on how to implement groupBy in vanilla JavaScript without…

The Map Function is one of the many Methods existing on the JavaScript Array prototype . If you want to do a deep dive on prototypical inheritance, here's a great read by Kyle Simpson on how…

The Road to React

Learn React by building real world applications. No setup configuration. No tooling. Plain React in 200+ pages of learning material. Learn React like 50.000+ readers.

Get it on Amazon.