0

I have a problem and I am not able to figure this one out. Lets say we have multiple arrays:

[1,2,3]
[1,2,3]
[11,12,13]

How could I extract all values on each index from multiple arrays and combine them into separate arrays?

Output should be:

[
   [1, 1, 11], // all items on index 1
   [2, 2, 12], // all items on index 2
   [3, 3, 13] // all items on index 3
]
CodeAndCode
  • 69
  • 1
  • 7

1 Answers1

1

The idea is to get ith elements of all subArrays in every iteration.

for example:

when i = 0, [firstSubArr[0], secondSubArr[0], thirdSubArr[0]] = [1,1,11];
    
when i = 1, [firstSubArr[1], secondSubArr[1], thirdSubArr[1]] = [2,2,12]; 

.........

const a = [1,2,3];
const b = [1,2,3];
const c = [11,12,13];

const combined = [a,b,c];
const ans = combined[0].map((_, i) => combined.map(row => row[i])); //Transposing 
console.log(ans);
Asraf
  • 1,322
  • 2
  • 12