In this blog post, You’ll learn to check the boolean value of an array in javascript or typescript.
Find Array contains a boolean value in javascript
The array contains a collection of similar type values.
For example, we have a User form that 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 the boolean value true that exists in the array of checkboxes in Angular,
This post talks about multiple ways of checking true/false values, that exist in an Array in Javascript/Typescript/Angular.
- For loop with if block
- Array some() method
- Array indexOf method
- Es7 Array Includes
All the examples will work in Javascript
, typescript
, and Angular
.
For loop with if block example
A basic developer will use the below logic to check the true value in an array
- Iterate the array used 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 a 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 an Array is an inbuilt method in javascript language.
- It iterates each element and calls the callback for each value,
- if the callback returns a true value, stop the iteration and returns true
- if the callback returns a false value, iteration goes to the 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 i.e latest javascript language introduced includes
method
It returns true if the value found 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