amitava mozumder
amitava mozumder

Reputation: 783

How to have one binary operation performed over a binary array?

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

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386883

You could use Boolean as callback for

var array = [true, true, false];

console.log(array.some(Boolean));  // or
console.log(array.every(Boolean)); // and

Upvotes: 3

haim770
haim770

Reputation: 49123

You can use every() for an AND:

arr.every(x => x);

And some() for OR:

arr.some(x => x);

Upvotes: 2

Sebastian Simon
Sebastian Simon

Reputation: 19545

Yes: for AND you use arr.every(bool => bool), for OR you use arr.some(bool => bool).

Upvotes: 2

Related Questions