Sometimes, we want to map multiple arrays with JavaScript.
In this article, we’ll look at how to map multiple arrays with JavaScript.
How to map multiple arrays with JavaScript?
To map multiple arrays with JavaScript, we can use the map
method.
For instance, we write:
const zip = (a1, a2) => a1.map((x, i) => [x, a2[i]]);
const arr1 = ['a', 'b', 'c'];
const arr2 = [1, 2, 3];
console.log(zip(arr1, arr2))
to define the zip
function that calls a1.map
to combine the entries from a1
and a2
with index i
into one entry.
Then we call zip
with arr1
and arr2
to zip them into one array.
As a result, the array logged is [["a", 1], ["b", 2], ["c", 3]]
.
Conclusion
To map multiple arrays with JavaScript, we can use the map
method.