How to check if Array Object contains the boolean value in javascript
- Admin
- Mar 10, 2024
- Typescript Javascript
This post talks about multiple ways of checking true/false values, that exist in an Array in Javascript/Typescript/Angular.
For loop
to iterate each element, check if an element is a Boolean value using theif statement
.Array
some()
method, Performs iteration of elements and checks at least one element found or notArray
indexOf
method, This returns number (-1,0,1, etc) check index withEs7 Array
Includes
that performs an element that exists in an array, returns true or false In this blog post, You’ll learn to check the Boolean value of an array in javascript or typescript.The array contains a collection of similar type values.
Multiple ways to check Array contains a boolean value in javascript?
For example, we have a User form that contains multiple checkboxes - which can select All/Deselect All in the Angular application.
A checkbox in FormGroup is defined with FormArray which holds- if the checkbox is checked, false - checkbox is not checked.
To implement form validation check at least one check box when there are multiple checkboxes.
It displays an error message if one of the checkboxes is not checked.
Then how do we check the boolean value true that exists in the array of checkboxes in Angular,
All the examples will work in Javascript
, typescript
, and Angular
.
Use 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 the loop
- if found, return true and break from the loop
- if not found, return a false value
// function return if an element is found in the array, else false
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
Use Array Some() method
Some()
in an Array is an inbuilt method in javascript that checks if one of the objects or elements found in an array, returns true.
- It iterates each element, calls the callback for each value,
- if the callback returns a
true
value, stop the iteration and returntrue
- if the callback returns a
false
value, the iteration goes to the next iteration until it returns true or the 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
using Array indexOf method to check with if else condition
indexOf method in Array
accepts input parameters, and returns index position if input is 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
Use ES7 Includes checking the true value
ES7 i.e latest javascript language introduced the includes
method
It returns true, if the value is 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
To summarize, Multiple approaches to check one element found in an array. You can choose one of the approaches based on your needs, coding styles, and readability.