1

I have two arrays that I'd like to combine, but in my searching I've only been able to find Array.concat.

let a = [1,2,3,4,5]
let b = [6,7,8,9,10]

How do I combine these to create the following?

let combine = [
  [1,6],
  [2,7],
  [3,8],
  [4,9],
  [5,10]
]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MayaGans
  • 1,815
  • 9
  • 30

1 Answers1

2

If both arrays have the same length, you can do the follow:

let a = [1,2,3,4,5]
let b = [6,7,8,9,10]
let combine = a.map((e, i) => [e, b[i]]);
console.log(combine);
zmag
  • 7,825
  • 12
  • 32
  • 42