Skip to content

zip

Combines multiple arrays element-by-element into an array of tuples. The result length equals the length of the shortest input array.

The inverse of unzip.

Available since v2.0.0

import { zip } from '@helpers4/array';
zip<A, B>(a: readonly A[], b: readonly B[]): [A, B][]
ParameterTypeDescription
areadonly A[]First array
breadonly B[]Second array

[A, B][] — Array of [a, b] pairs

Combine two arrays element-by-element.

zip(['a', 'b', 'c'], [1, 2, 3])
// => [['a', 1], ['b', 2], ['c', 3]]

Stops at the end of the shorter array to avoid undefined entries.

zip([1, 2, 3], ['x', 'y'])
// => [[1, 'x'], [2, 'y']]

View source on GitHub