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]

相关文章:

  • 2022-12-23
  • 2021-07-27
  • 2022-12-23
  • 2021-07-10
  • 2022-01-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2021-11-21
相关资源
相似解决方案