Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 468 Bytes

transpose.md

File metadata and controls

25 lines (19 loc) · 468 Bytes
标题 标签
transpose(矩阵转换) array(数组)

转置一个二维数组。

  • 使用 Array.prototype.map() 创建给定二维数组的转置。
const transpose = arr => arr[0].map((col, i) => arr.map(row => row[i]));

调用方式:

transpose([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [10, 11, 12]
]);
// [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]

应用场景