Open lixiaojuan12 opened 4 years ago
实现:[1, 2, [3], [4, 5, [6, [7, 8, 9]]]] => [1,2,3,4,5,6,7,8,9,]
[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));
实现:
[1, 2, [3], [4, 5, [6, [7, 8, 9]]]] => [1,2,3,4,5,6,7,8,9,]