let a = [1, 2, 3, [4, 5, 6, [7, 8], 9], 10] console.log(flattenDeep(a))
这是什么高级方法 !!!
forEach
1 2 3 4 5 6 7 8 9 10 11 12 13 14
functionflattenDeep(arr) { let res = [] arr.forEach(item => { if (Array.isArray(item)) { res = res.concat(flattenDeep((item))) } else { res.push(item) } }) return res }
let a = [1, 2, 3, [4, 5, 6, [7, 8], 9], 10] console.log(flattenDeep(a))