js 数字平铺flat的实现
function flat(arr, count) {
if(count === 0) {
return arr
}
return arr.reduce((data, next) => {
if(Array.isArray(next)) {
return data.concat(flat(next, count - 1))
} else {
return data.concat(next)
}
}, [])
}
let flatArr = [1, 2, [3, 4, [5, 6]],[7, 8, [9, 12, 13, [88, 99]]]]
let res = flat(flatArr, 1)
console.log(res)