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://www.cnblogs.com/roseAT/p/12305937.html