let arr = [[0,1],[1,2,3],[4,[1,23]]]
const newArr = function (arr) {
return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? newArr(cur) : cur), [])
};
console.log(newArr(arr));
let arr = [
[0, 1],
[1, 2, 3],
[3, 5, 0],
];
let newArr = arr.reduce((pre, cur) => {
console.log(cur);
return pre.concat(cur);
}, []);
console.log("==============================");
console.log(newArr);
利用reduce将多维数组转变为一维数组
最新推荐文章于 2023-07-19 11:44:54 发布