Search code examples
javascriptarrays

Zip arrays in JavaScript?


I have 2 arrays:

var a = [1, 2, 3]
var b = [a, b, c]

What I want to get as a result is:

[[1, a], [2, b], [3, c]]

It seems simple but I just can't figure out.

I want the result to be one array with each of the elements from the two arrays zipped together.


Solution

  • Use the map method:

    var a = [1, 2, 3]
    var b = ['a', 'b', 'c']
    
    var c = a.map(function(e, i) {
      return [e, b[i]];
    });
    
    console.log(c)

    DEMO