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());
数组递归降维代码
数组递归降维代码
扫描二维码,在手机上阅读
推荐阅读: