Transposing a 2D-array in JavaScript

后端 未结 23 2918
难免孤独
难免孤独 2020-11-22 01:40

I\'ve got an array of arrays, something like:

[
    [1,2,3],
    [1,2,3],
    [1,2,3],
]

I would like to transpose it to get the following

23条回答
  •  旧时难觅i
    2020-11-22 02:31

    const transpose = array => array[0].map((r, i) => array.map(c => c[i]));
    console.log(transpose([[2, 3, 4], [5, 6, 7]]));

提交回复
热议问题