JavaScript

Count Elements in an Array That Match a Condition Using JavaScript

An array is a common data structure to store data in JavaScript. Sometimes, developers want to count the elements in an array on a specific condition, such as how many positive numbers or negative numbers exist in the array, the even/odd numbers from the number array, and so on.

This post will describe the methods for counting elements in an array that matches the particular condition in JavaScript.

How to Count Elements in an Array that Match/Satisfy the Condition Using JavaScript?

To count elements in an array that matches the specific condition, use the following methods:

Method 1: Count Elements in an Array that Match a Condition Using filter() Method With length Property

To count elements in an array based on condition, use the “length” property with the “filter()” method. The filter() method iterates the array’s elements and checks the specified condition, and then the length property gets the count of the number of existing elements that match the condition. The filter() method gives an array of true values returned by the callback function of the original array.

Syntax

Use the given syntax for the filter() method with length property for counting the elements in an array:

filter((element, index) => { // Statements }).length

The “filter()” method takes two parameters “element” and “index” and calls a call back function:

  • element” is the current element of the array that is being processed.
  • index” is the position of the current element.
  • The callback function executes every array’s element, and it returns value to the filter() method.

Example

First, create an array of numbers:

var array = [-8, -4, -2, 0, 2, 4, 6, 8, 10];

Call the filter() method and count the elements greater than 0 by checking the condition:

const count = array.filter(arr => {

if (arr > 0) {

return true;

}

return false;

}).length;

Print the resultant count on the console:

console.log(count);

The output displays “5”, which means there are 5 elements in an array greater than 0:

Method 2: Count Elements in an Array that Match a Condition Using reduce() Method

For counting elements in an array based on condition, there is another method called the “reduce()” method. The reduce() method first calls a callback function on each array’s element and gives a single value as an output.

Syntax

The syntax of reduce() method is as follows:

reduce((accumulator, currentValue, currentIndex, array) => { // statement }

The “reduce()” method takes four parameters and invokes a call back function:

  • accumulator” is the resultant value of the previous call of the callback function.
  • currentValue” is the current element that is being processed.
  • currentIndex” is the position of the current element.
  • array” is the specified array.
  • Here, in the given example, we will say “elements” as an accumulator and “arr” as an array where the reduce() method is called upon.

Example

Call the reduce() method on the array and count the elements by checking the array elements greater than 0. If the condition is true, the callback function adds 1 in the existing element and returns to the “reduce()” method:

const count = array.reduce((elements, arr) => {

if (arr > 0) {

return elements + 1;

}

return elements;

}, 0);

Finally, print the count on the console:

console.log(count);

Output

We have gathered essential instructions relevant to counting the array elements based on the specific condition.

Conclusion

To count array elements, use the “length” property with the “filter()” method and “reduce()” method. Both reduce() and filter() methods use a callback function that will execute for every array’s element and return values to the method. In this post, we described the methods for counting elements in an array that matches the particular condition in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.