题解 | #数组扁平化#

数组扁平化

https://www.nowcoder.com/practice/5d7e0cf4634344c98e6ae4eaa2336bed

本示例仅仅用于手写的代码,别的方法不作细说,改方法仅适用与参数只有 ‘简单数据类型’ 和 数组,别的复杂数据类型不做处理

const _flatten = arr => {
  // 补全代码
  function handleFormat(option){
    let sunArray = []
    if (Array.isArray(option)) {
      option.forEach(v => {
        if (Array.isArray(v)) {
          sunArray.push(...handleFormat(v))
        } else {
          sunArray.push(v)
        }
      })
    } else {
      sunArray.push(option)
    }
    return sunArray
  }
  return handleFormat(arr)
}

全部评论

相关推荐

07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务