«

数组递归降维代码

六思逸 发布于 阅读:4131 JavaScript


Array.prototype.myReduce = function () {
let newArr = [];
for (let item of this) {
   if (Array.isArray(item)) {
       newArr = newArr.concat(item.myReduce());
        } else {
            newArr.push(item);
            }
    }
return newArr;  
};
// 多维数组
let arr = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
 console.log(arr.myReduce());

数组递归降维代码


扫描二维码,在手机上阅读
收到1条评论
avatar
小小的人 1 年前
你干嘛
回复