方法:flat();可接受参数2,3,4.....,Infinity;
用法如下:
const a = [1, 2, 3, 4, 3];
const b = [3, 4, [5, 6]];
const c = [3, 4, [5, 6, [7, 8]]];
// 不传默认是二维数组降一维数组
console.log(a.concat(b).flat()); // 打印为[1, 2, 3, 4, 3, 3, 4, 5, 6]
console.log(a.concat(c).flat(3)) // 打印为[1, 2, 3, 4, 3, 3, 4, 5, 6, 7, 8]
// 如果数组嵌套太过复杂可直接传值Infinity
console.log(a.concat(c).flat(Infinity)) // 打印为[1, 2, 3, 4, 3, 3, 4, 5, 6, 7, 8]