本文介绍了在JavaScript中逻辑与两个布尔数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ES6中将两个布尔数组相加的和好的优雅的功能性解决方案是什么?

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]

应导致:

[true, false, false]

推荐答案

使用可以使用 Array#map 迭代第一个数组,并使用索引(回调中的第二个参数)获取第二个数组的值:

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

这篇关于在JavaScript中逻辑与两个布尔数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:41