题解 | #数组扁平化#

数组扁平化

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

// 方法一,reduce + 递归
const _flatten = arr => {
  // 补全代码
  return arr.reduce((pre, current)=>{
	Array.isArray(current) ? pre.push(..._flatten(current)) : pre.push(current)
    return pre
  }, [])
}

// 方法三
const _flatten = arr => {
  // arr.toString() 的结果为 1,2,3,4 
  return arr.toString().split(',').map(item=> Number(item))
}

// 方法二,flat,此方法用例不通过,但是可以拿到想要的结果
const _flatten = arr => {
  return arr.flat(Infinity)
}

全部评论

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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