flat

;(() => {
  function myFlat(arr) {
    const result = []
 
    arr.forEach(item => {
      if (Array.isArray(item)) {
        result.push(...myFlat(item))
      } else {
        result.push(item)
      }
    })
 
    return result
  }
 
  function test() {
    const arr1 = [1, [2], [[3], 3], [[[4], 4], 4]]
    console.log(myFlat(arr1))
  }
 
  test()
})()
(() => {
  function myFlat(arr, depth) {
    depth = depth ?? Infinity // 参数归一化
 
    if (depth === 0) return arr // 如果深度为0的话,返回本身
 
    const result = []
 
    arr.forEach(item => {
      if (Array.isArray(item)) {
        result.push(...myFlat(item, depth - 1))
      } else {
        result.push(item)
      }
    })
 
    return result
  }
 
  function test() {
    const arr1 = [1, [2], [[3], 3], [[[4], 4], 4]]
    console.log(myFlat(arr1))
    console.log(myFlat(arr1, 0))
    console.log("入参为1:", myFlat(arr1, 1))
    console.log("入参为2:", myFlat(arr1, 2))
    console.log(myFlat(arr1, Infinity))
  }
 
  test()
})()