// 举例1
// [[1, 2, 3], [1, 2, 3], [1, 2, 3]] --> [1, 2, 3, 1, 2, 3, 1, 2, 3]
// 举例2
// [1, 2, 3, 1, 2, 3, 1, 2, 3] --> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
// 一维变二维
function splitArray(arr, spanLen){
var arrLen = arr.length;
var result = [];
for(let i = 0, l = arrLen; i < l ; i += spanLen){
result.push(arr.slice( i, i + spanLen));
};
return result;
}
//二维变一维
function flatArray(arr){
var arrLen = arr.length;
var result = [];
for(let i = 0, l = arrLen; i < l ; i ++){
result = result.concat(arr[i]);
};
return result;
}
var array2D = [[1, 2, 3], [1, 2, 3], [1, 2, 3]];
console.log("我是二维数组:" );
console.log(array2D);
var array2DResult = flatArray(array2D);
console.log("我是二维转一维计算结果:");
console.log(array2DResult);
var array1D = [1, 2, 3, 1, 2, 3, 1, 2, 3];
console.log("我是一维数组:");
console.log(array1D);
var array1DResult = splitArray(array1D,3);
console.log("我是一维转二维计算结果:");
console.log(array1DResult);
console