0

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,030
  • 21
  • 100
  • 124
MayaGans
  • 1,649
  • 5
  • 19

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,281
  • 12
  • 30
  • 38