I have a nested array like this.
var arr = [[false,false,false,false],[false,false,false,false],[false,false,false,false],[false,false,false,false]]
I want to check if every value is false. I could think of one way of doing this.
let sum = 0;
arr.forEach((row, i) => {
row.forEach((col, j) => {
sum = sum +arr[i][j]
});
});
if(sum === 0){
console.log("all values false")
}
This works. But I'm curious if there is a better way? to check if all values are true or false?
You could take two nested Array#some
, because if you found one true
value the iteration stops. Then take the negated value.
var array = [[false, false, false, false], [false, false, false, false], [false, false, false, false], [false, false, false, false]],
result = !array.some(a => a.some(Boolean));
console.log(result);