js实现展开多级数组
1.递归
function steamrollArray(arr) {
let res = []
for (const a of arr) {
if(a instanceof Array){
res = res.concat(steamrollArray(a))
}else{
res.push(a)
}
}
return res;
}
console.log(steamrollArray([1, [2], [3, [[4]]]]));// [1,2,3,4]
2.Array.prototype.flat
console.log([1, [2], [3, [[4]]]].flat(Infinity)); // [1,2,3,4]
"你的指尖,拥有改变世界的力量! "
欢迎关注我的个人博客:https://sugarat.top