数组扁平化

79 阅读1分钟
function flatArr(arr){
    let res = []
    arr.forEach((item) => {
        if(Array.isArray(item)){
            item.forEach(n => res.push(n))
        }else{
            res.push(item)
        }
    })
    return res
}
function flatArrDeep(arr){
    let res = []
    arr.forEach((item) => {
        if(Array.isArray(item)){
            let flatItem = flatArrDeep(item)
            flatItem.forEach(n => res.push(n))
        }else{
            res.push(item)
        }
    })
    return res
}
function flatten2(arr: any[]): any[] {
  let res: any[] = []
  arr.forEach(item => {
    res = res.concat(item)
  })
  return res
}