Reputation: 783
Suppose a binary array var arr = [true, true, false];
.
Is there any way to get the AND
or OR
of the whole array using one method?
Upvotes: 4
Views: 81
Reputation: 386883
You could use Boolean
as callback for
OR with Array#some
or for
AND with Array#every
.
var array = [true, true, false];
console.log(array.some(Boolean)); // or
console.log(array.every(Boolean)); // and
Upvotes: 3
Reputation: 49123
You can use every()
for an AND:
arr.every(x => x);
And some()
for OR:
arr.some(x => x);
Upvotes: 2
Reputation: 19545
Yes: for AND
you use arr.every(bool => bool)
, for OR
you use arr.some(bool => bool)
.
Upvotes: 2