方式1:
const arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
//定义函数,降低数组维度
function fn(myArr) {
const newArr = [];
//遍历外层
for (let i = 0; i < myArr.length; i++) {
//遍历内层
for (let j = 0; j < myArr[i].length; j++) {
newArr.push(myArr[i][j]);
}
}
return newArr;
}
console.log(fn(arr));
方式2:
const arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
//定义函数,降低数组维度
function fn(myArr) {
/**
* concat 拼接数组
* apply 改变this指向
*
* [].concat([],[],[])
*/
return Array.prototype.concat.apply([], myArr);
}
console.log(fn(arr));
方式3:
const arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
let [
[a, b, c],
[d, e, f],
[g, h, j]
] = arr;
console.log([].concat(a, b, c, d, e, f, g, h, j));