0

Hi guys I have this array :

[[2,3,0],[0,4,5]]

and i want traverse this array like:

[[2,0],
 [3,4],
 [0,5]]

Any recommendations please? I am working with javascript

Sigmond Gatt
  • 73
  • 10

1 Answers1

1

You can easily achieve this result using the Array.prototype.map

const arr = [
  [2, 3, 0],
  [0, 4, 5],
];

const result = arr[0].map((val, i) => {
  return Array(arr.length)
    .fill("")
    .map((_, index) => arr[index][i]);
});

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42