现有:
var arr1 = [[30,-1,90],[70,100,-40],[39,29,6],[39,92,9]]; var arr2 = [];
理想变成如下:
var2 = [ [30,70,39,39], [-1,100,29,92], [90,-40,6,9] ]
最笨的方法:
// 确定新bai数组du有多少zhi行 for(var i=0;i<arr1[0].length;i++){ arr2[i] = []; } //遍历原数组 for(var i=0;i<arr1.length;i++){ for(var j=0;j<arr1[i].length;j++){ arr2[j][i] = arr1[i][j]; } } console.log(arr2);
一个高效方法:
var newArray = arr1[0].map(function (col, i) { return arr1.map(function (row) { return row[i]; }) }); console.log(newArray);
运行结果: