Categories
JavaScript Answers

How to zip two arrays in JavaScript?

Spread the love

To zip two arrays in JavaScript, we use the map method.

For instance, we write

const a = [1, 2, 3];
const b = ["a", "b", "c"];

const c = a.map((e, i) => {
  return [e, b[i]];
});

console.log(c);

to call a.map with a callback that returns an array with arrays of items in a and the items in b at the same position as the item in a.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *