数组的扁平化两种方法

tech2025-09-12  24

const arr = [1, 2, [3, 4, [5, 6]]]

方法一 console.log(arr.flat(Infinity)) 方法二 function flatFn(arr) { return arr.reduce((prev, next) => { return prev.concat(Array.isArray(next) ? flatten(next) : next) }, []) }

console.log(flatFn(arr))
最新回复(0)