将数组平铺到指定深度

const flatten = (arr, depth = 1) =>
  depth != 1
    ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), [])
    : arr.reduce((a, v) => a.concat(v), [])

posted @ 2021-08-05 16:21  吴小明-  阅读(59)  评论(0编辑  收藏  举报