1

so i have these three arrays:


  var a = ["one", "two", "three"];
  var b = ["a", "b", "c"];
  var c = ["1", "2", "3"];

i want to combine them like so:

  var d = [
    ["one", "a", "1"],
    ["two", "b", "2"],
    ["three", "c", "3"],
  ];

how do i do this? I know i need a foor loop but i cant seem to understand how to set it up. Thanks for any answers!

Adam Baser
  • 115
  • 7

2 Answers2

1

Do like:

const a = ['one', 'two', 'three'], b = ['a', 'b', 'c'], c = ['1', '2', '3'], combo = [];
for(let i=0,l=a.length; i<l; i++){
  combo.push([a[i], b[i], c[i]]);
}
console.log(combo);
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

Assuming all the arrays are the same length:

First combine them into one array:

let combined = [a, b, c];

Then iterate through them all at the same time:

var d = [];

for (var i = 0; i < combined[0].length; i ++) {
    d.push(combined.map(arr => arr[i]));
}
Aplet123
  • 33,825
  • 1
  • 29
  • 55