lixiaojuan12 / Summary-of-code

手写代码爽起来~~~
0 stars 0 forks source link

14. 数组扁平化 #14

Open lixiaojuan12 opened 4 years ago

lixiaojuan12 commented 4 years ago

实现:[1, 2, [3], [4, 5, [6, [7, 8, 9]]]] => [1,2,3,4,5,6,7,8,9,]

var arr = [1, 2, [3], [4, 5, [6, [7, 8, 9]]]];
function flatten(arr) {
    return arr.reduce((res, next) => {
        return res.concat(Array.isArray(next) ? flatten(next) : next);
    }, []);
}
console.log(flatten(arr));