# Combining arrays pairwise

Posted on

When values in two arrays are related pairwise, we can combine them into a single array of pairs. We’re stepping through both arrays at the same time, combining the values we find into pairs.

This operation is called “zip” because it works its way through both arrays like a zipper.

``````// takes two arrays and combines their elements pairwise
const zip = (firstArray, secondArray) => {
const longerLength = Math.max(firstArray.length, secondArray.length)
const indices = [...new Array(longerLength).keys()]

return indices.reduce((list, index) => [
...list,
[firstArray[index], secondArray[index]]
], [])
}

zip([4, "8 oz", "2 cups", "some"], ["eggs", "milk", "flour", "salt"])
// ⇒ [[4, "eggs"], ["8 oz", "milk"], ["2 cups", "flour"], ["some", "salt"]]

// if one array is shorter, that list’s value in a pair is `undefined`
zip([1, 2, 3], ["a", "b"])       // ⇒ [[1, "a"], [2, "b"], [3, undefined]]
zip([1, 2],    ["a", "b", "c"])  // ⇒ [[1, "a"], [2, "b"], [undefined, "c"]]``````
``````// takes two arrays and combines their elements pairwise
const zip = (firstArray, secondArray) => {
const longerLength = Math.max(firstArray.length, secondArray.length)
const indices = [...new Array(longerLength).keys()]

return indices.reduce((list, index) => [
...list,
[firstArray[index], secondArray[index]]
], [])
}

zip([4, "8 oz", "2 cups", "some"], ["eggs", "milk", "flour", "salt"])
// ⇒ [[4, "eggs"], ["8 oz", "milk"], ["2 cups", "flour"], ["some", "salt"]]

// if one array is shorter, that list’s value in a pair is `undefined`
zip([1, 2, 3], ["a", "b"])       // ⇒ [[1, "a"], [2, "b"], [3, undefined]]
zip([1, 2],    ["a", "b", "c"])  // ⇒ [[1, "a"], [2, "b"], [undefined, "c"]]``````

## Legal

© 2024 Dominik Habersack. All rights reserved.

Debug
none
Grid overlay