JavaScript将多个布尔数组与OR ||合并 算子
我们有一个布尔数组的数组,像这样-
const arr = [[true,false,false],[false,false,false],[false,false,true]];
我们需要编写一个函数,通过使用OR(||)运算符组合每个子数组的相应元素,从而将该数组的数组合并为一维数组。
让我们为该函数编写代码。我们将使用Array.prototype.reduce()函数来实现此目的。
示例
const arr = [[true,false,false],[false,false,false],[false,false,true]];
const orMerge = arr => {
return arr.reduce((acc, val) => {
val.forEach((bool, ind) => acc[ind] = acc[ind] || bool);
return acc;
}, []);
};
console.log(orMerge(arr));输出结果
控制台中的输出将为-
[ true, false, true ]
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短