-2

I'm trying to create arrays from arrays. I don't know how many arrays there are going to be, but here's an example.

const array0 = ['item0', 'item1', 'item2'];
const array1 = ['item0', 'item1', 'item2'];
const array2 = ['item0', 'item1', 'item2'];

How can I create 3 new arrays from these arrays like this?

const array0 = ['item0', 'item0', 'item0'];
const array1 = ['item1', 'item1', 'item1'];
const array2 = ['item2', 'item2', 'item2'];
CloudBranch
  • 1,434
  • 3
  • 18
  • 23
  • https://stackoverflow.com/q/22015684/438992 but then you'd split apart the array zip result. But you won't be able to do it like you want, since you don't know how many arrays there will be. – Dave Newton Aug 23 '19 at 21:05
  • are the arrays always ordered like this? – karthick Aug 23 '19 at 21:06
  • 2
    Possible duplicate of [Transposing a 2D-array in JavaScript](https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript) – Michelangelo Aug 23 '19 at 21:14

1 Answers1

0

This function should work:

var transpose=(...a)=>a[0].map((b,c)=>a.map(d=>d[c]))

Map always makes it easier to loop through things.

Kavelin
  • 397
  • 1
  • 11