Js之二维数组横纵x轴y轴置换

Js之二维数组横纵x轴y轴置换

话不多说上代码

// 原始数组
let data = [
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  [0, 0, 25, 69, 6, 0, 0, 0, 5, 7, 0, 210, 4, 0, 27, 0, 0, 0]
  [0, 0, 0, 54, 4, 0, 0, 0, 5, 2, 0, 80, 0, 0, 11, 0, 1, 0]
]
// 定义置换后的数组
let dataRes = []
for (let j = 0; j < data[0].length; j++) {
  dataRes[j] = [];
  for (let i = 0; i < data.length; i++) {
    dataRes[j][i] = data[i][j];
  }

}
// 输出结果
console.log(data)
console.log(dataRes)

效果

data.png
dataRes.png

Last modification:June 2nd, 2020 at 12:01 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment