只扁平一层
[1, 2, [3, [4, [5, 6]]]] ===> [ 1, 2, 3, [ 4, [ 5, 6 ] ] ]
const flattenDeep = (arr = []) => {
return arr.reduce((pre, cur) => pre.concat(cur), [])
}
全部扁平
[1, 2, [3, [4, [5, 6]]]] ===> [ 1, 2, 3, 4, 5, 6 ]
const flattenDeep = (arr = []) => {
return arr.reduce((pre, cur) => {
return pre.concat(Array.isArray(cur) ? flattenDeep(cur) : cur)
}, [])
}
网友评论