潇湘沐
就像任何其他语言一样:int[][] copy = new int[columns][rows];for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { copy[j][i] = original[i][j]; }}您只需在JS中以不同方式构造2D数组。像这样:function transpose(original) { var copy = []; for (var i = 0; i < original.length; ++i) { for (var j = 0; j < original[i].length; ++j) { // skip undefined values to preserve sparse array if (original[i][j] === undefined) continue; // create row if it doesn't exist yet if (copy[j] === undefined) copy[j] = []; // swap the x and y coords for the copy copy[j][i] = original[i][j]; } } return copy;}console.log(transpose([ [1,2,3], [4,5,6], [7,8,9]]));