function flatten(arr) {
return arr.reduce((pre, cur) => pre.concat(Array.isArray(cur) ? flatten(cur) : cur), [])
}
console.log(flatten(arr))
4. 扩展运算符
function flatten(arr) {
while (arr.some(item => Array.isArray(item))) {
console.log(arr, ‘kkk‘)
arr = [].concat(...arr)
console.log(arr, ‘lll‘)
}
return arr
}
console.log(flatten(arr))