Logical anding two boolean arrays in javascript?

后端 未结 1 1385
余生分开走
余生分开走 2021-01-14 18:26

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]
         


        
1条回答
  •  执笔经年
    2021-01-14 18:39

    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);

    0 讨论(0)
提交回复
热议问题