In this post, we are going to learn how to Count True in a JavaScript array with examples by using some array function filter(),reduce(),for of and foreach loop.We will define a custom function to make the code resuable.
1. Count True in JavaScript array using array.filter()
The Javascript array.filter() method call a callback function for each element of array and return an array of elements that satisfied the condition set by callabck function. In this JavaScript Program, we have set a condition where array element are equal to True,So it return an array elements that satisfy condition and Length property is used to get count of elements in return array.
let bolArr = [true,true, true, false, true,true];
const Total = bolArr.filter(value => value === true).length;
console.log('Total True value in array:',Total);
Output
Total True value in array: 5
2. Boolean object to Count True in JavaScript array
To get the count of truth values we will pass Boolean object as parameter to array.filter() method and array length property return count of True values in given array.
let bolArr = [true,true, true, undefined, true,true,NaN,false];
const Total = bolArr.filter(Boolean).length;
console.log('Total True value in array:',Total);
Output
Total True value in array: 5
3. foreach loop to Count True in JavaScript array
In this Javascript Program We have used the foreach loop to count all the True values in JavaScript array.The foreach iterate over each element of array.If the value of array element is True then values of count will increase by 1 this is how we will get the Count of True value in JavaScript array
let bolArr = [true,true, true, false, true,true];
let count =0
bolArr.forEach(element => element ? count++ : element )
console.log('Total True value in array:',count);
Output
Total True value in array: 5
4. reduce() to Count True in JavaScript array
The array.reduce() method call a callback function for each element of array and result return by callback function after each iteration pass to accumulator of reduce function that used for next iteration,Finally after iteration is over we will get count of Total True values in given array.Let us understand with below example.
let bolArr = [true,true, true, false, true,true];
const count = bolArr.reduce((acc, current) => acc + current, 0);
console.log('Total True value in array:',count);
Output
Total True value in array: 5
5. For of loop to Count True in JavaScript array
In this javascript program example we have used for of loop to iterate over each elemnt of array and used if condition to check if the element is equal to True.When values of array elemnt is true ,We have increased count by 1 ,once the iteration is over we will get all the True values in given array.
let bolArr = [true,true, true, false, true,true];
let count = 0;
for(let element of bolArr)
if(element===true)
{
count++;
}
console.log('Total True value in array:',count);
Output
Total True value in array: 5
6. Custom function with for loop
In this JavaScript program we have define a custom function to make the code reusable and pass the given array as parameter to this custom function.we have used for loop to iterate over each elemnt of array and used if condition to check if element is equal to True.When values of array element is True ,We have increased count by 1 ,once the iteration is over function return count of total True values in given array
let bolArr = [true,true, true, false, true,true];
let count = 0;
function ArrCountTrue(arr) {
for(let i = 0; i < arr.length; i++)
{
if (arr[i] === true)
count++;
}
return count;
}
console.log('Total True value in array:',ArrCountTrue(bolArr));
Output
Total True value in array: 5
Summary
In this post we have learn how to count Count True in JavaScript array with different program example by using some array function filter(),reduce(),for of and foreach loop.