Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logical anding two boolean arrays in javascript?

What would be a nice elegant functional solution for anding two boolean arrays in ES6?

const a1 = [true, false, false]
const a2 = [true, true, false]

should result in:

[true, false, false]
like image 225
Dachstein Avatar asked Oct 23 '25 15:10

Dachstein


1 Answers

Use can use Array#map to iterate the 1st array, and get the value of the 2nd array using the index (the 2nd param in the callback):

const a1 = [true, false, false]
const a2 = [true, true, false]

const result = a1.map((b, i) => b && a2[i]);

console.log(result);
like image 96
Ori Drori Avatar answered Oct 26 '25 05:10

Ori Drori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!