How to check boolean value of an array in javascript/typescript/angular


In this blog post, You’ll learn to check the boolean value of an array in javascript or typescript.

Find Array contans boolean value in javascript

The array contains a collection of similar type values. For example, we have a User form which contains multiple checkboxes - which can have select All/Deselect ALl in Angular application, A checkbox in FormGroup is defined with FormArray which holds true- if the checkbox is checked, false - checkbox is not checked.

To implement form validation like if a user is not checked any checkbox, display error message - Please check at least one checkbox.

Then how do we check true value present in the array of checkboxes in Angular,

This post talks about  multiple ways of checking true/false values exists in an Array in Javascript/Typescript/Angular

  • Forloop wih if block
  • Array some() method
  • Array indexOf method
  • Es7 Array Includes

All the examples will work in Javascript,typescript and Angular.

Forloop wih if block example

A basic developer will use the below logic to check the true value in an array

  • Iterate the array using for the normal loop
  • Check each element for true value using if the loop
  • if found, return true and break from the loop
  • if not found, return false value
function checkTrueExistsArray(array){
for(var k=0; k<array.length; k++){
    if(array[k]){
        return true
        break;
    }
}
return false;
}
var arrayVariable= [false,false,true,false,true]
var arrayVariable1= [false,false,false,false,false]

console.log(checkTrueExistsArray(arrayVariable)) //true
console.log(checkTrueExistsArray(arrayVariable1)) //false

Array Some() method

Some() in a Array is a inbuilt method in javascript language

  • It iterates each element and calls the callback for each value,
  • if the callback returns true value, stop the iteration and returns true
  • if the callback returns a false value, iteration goes to next iteration until it returns true or array iteration is done.
function checkTrueUsingArraySome(array){
  if(array.some((item)=> item === true)){
    return true;
    }
    return false;
}
var arrayVariable= [false,false,true,false,true]
var arrayVariable1= [false,false,false,false,false]
console.log(checkTrueUsingArraySome(arrayVariable)) //true
console.log(checkTrueUsingArraySome(arrayVariable1)) //false

Array indexOf method example

indexOf method in Array accepts input parameters,  returns index position if input found in an array, else returns -1

function checkTrueUsingArrayIndexOf(array){
  if(array.indexOf(true) != -1){
    return true;
    }
    return false;
}
var arrayVariable= [false,false,true,false,true]
var arrayVariable1= [false,false,false,false,false]

console.log(checkTrueUsingArrayIndexOf(arrayVariable)) //true
console.log(checkTrueUsingArrayIndexOf(arrayVariable1)) //false

Es7 Includes checking true value

ES7 ie latest javascript language introduced includes method

It returns true if value founds in an array else returns false, You can check more about es7 includes

function checkTrueUsingArrayInclude(array){
  if(array.includes(true)){
    return true;
    }
    return false;
}
var arrayVariable= [false,false,true,false,true]
var arrayVariable1= [false,false,false,false,false]

console.log(checkTrueUsingArrayInclude(arrayVariable)) //true
console.log(checkTrueUsingArrayInclude(arrayVariable1)) //false
Similar Posts