Count Elements In Array That Match A Condition In JavaScript

To count elements in array that match a condition in JavaScript, you can use any of the following methods –

  1. Use Array.filter() method to filter all the elements that match the given condition and then use the length property to get the count.
  2. Use Array.reduce() method to increment a counter for each element that matches the given condition.
  3. Use Array.forEach() method and increment a counter inside the callback function for each element that matches the given condition.

Let’s look at each of these methods in detail below.

Count Elements In Array That Match A Condition In JavaScript

count elements in array that match a condition in javascript

Use Array.filter() method

The Array.filter() method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument function.

We can use this method to filter all the elements that match a given condition and then use the length property to get the count. The following code snippet shows how to do this –

var arr = [{id:1}, {id:2}, {id:3}];

var count = arr.filter(element => {

if(element.id > 1) {

  return true;

 }

 return false;

}).length;

console.log(count); //2

Use Array.reduce() method

Another way to count the number of elements in an array that match a given condition is to use the Array.reduce() method. This method takes a function as an argument which is invoked for each element in the array. It also takes an optional second argument which represents the initial value of the accumulator variable.

The accumulator variable is used to store the intermediate results and it’s value is returned by the function. In our case, we can use this accumulator variable to increment a counter for each element that matches the given condition. The following code snippet shows how to do this –

const countElements = (arr, condition) => arr.reduce((accumulator, currentValue) => {

if(condition(currentValue)) {

  accumulator++;

}

return accumulator;

}, 0);

Here’s the full code –

var arr = [{id:1}, {id:2}, {id:3}];

var count = arr.reduce((accumulator, obj) => {

if(obj.id>1) {

  return accumulator + 1;

 }

 return accumulator;

}, 0);

console.log(count); //2

Use Array.forEach() method

Another way to count elements in array that match a condition in JavaScript is to use the Array.forEach() method. The forEach() method executes a provided function once per array element.

We can use this method and increment a counter inside the callback function for each element that matches the given condition.

The following code shows that –

var arr = [{id:1}, {id:2}, {id:3}];

var count = 0;

arr.forEach(element => {

if(element.id>1) {

  count+=1;

 }

});

console.log(count); //2

These were some of the ways in which you can count elements in array that match a condition in JavaScript. Do let us know if you have any other methods to achieve the same. Thanks for reading! 🙂

Leave a Reply