js二维数组行列互换()

const aa = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
function transfer (aa) {
  const newArr = aa[0].map((col, i) => {
    return aa.map(row => {
      return row[i]
    })
  });
  return newArr;
}

const bb = transfer(aa)

简写 一行代码实现

const cc = aa[0].map((_, i) => aa.map(row => row[i]))
————————
const aa = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
function transfer (aa) {
  const newArr = aa[0].map((col, i) => {
    return aa.map(row => {
      return row[i]
    })
  });
  return newArr;
}

const bb = transfer(aa)

简写 一行代码实现

const cc = aa[0].map((_, i) => aa.map(row => row[i]))