假设我们有一个布尔数组的数组,如下所示:
const arr = [[true,false,false],[false,false,false],[false,false,true]];
我们需要编写一个函数,通过使用AND(&&)运算符将每个子数组的相应元素组合在一起,将该数组的数组合并为一维数组。
让我们为该函数编写代码。我们将使用Array.prototype.reduce()函数来实现此目的。
示例
以下是代码-
const arr = [[true,false,false],[false,false,false],[false,false,true]]; const andMerge = (arr = []) => { return arr.reduce((acc, val) => { val.forEach((bool, ind) => { acc[ind] = acc[ind] && bool || false; }); return acc; }, []); }; console.log(andMerge(arr));
输出结果
这将在控制台中产生以下输出-
[ false, false, false ]
热门文章
- 使用Jenkins实现前端自动化打包部署(Linux版本)
- springboot整合SpringSecurity实现认证、授权功能简单入门案例
- 国内排名前十的兽药厂家 国内排名前十的兽药厂家有哪些
- 宠物粮食哪里批发(宠物粮进货平台)
- Error response from daemon: Get https://registry-1.docker.io/v2/ … read: connection refused
- redis之发布与订阅(publish/subscribe模式)
- 1月21日 - 最高速度20.5M/S,2025年Nekobox每天更新免费节点订阅地址
- js模块 – amd cmd commonjs esm umd
- Vue基础之Vue列表渲染
- encodeURI encodeURIComponent 的区别,和使用场景