let a = [1, 2, 3, [5, 6, 7, 8, [9, 10, 11, [12, 13, 14, [15, 16]]]]];
偏方
let a = [1, 2, 3, [4, 5]];
let b = a.join().split(",");
let d = (a + "").split(",");
let c = a.toString().split(",");
console.log(b); // ["1", "2", "3", "4", "5"]
b.map(item=>Number(item));// [1, 2, 3, 4, 5]
console.log(b); // ["1", "2", "3", "4", "5"]
d = d.map(parseFloat); // [1, 2, 3, 4, 5]
console.log(d);
递归方式
function flatten(arr) {
return arr.reduce((total, current) => {
if (Array.isArray(current)) {
total = total.concat(flatten(current));
} else {
total.push(current);
}
return total;
}, []);
}
console.log(flatten(a));