Zip multiple arrays
Pair the n-th items of several arrays.
function zip(arrays) { return Array.from({ length: Math.max(0, ...arrays.map(array => array.length)) }, (_, i) => arrays.map(array => array[i] ?? null)); }
Input & expected output
Input
[
[
"[email protected]",
"[email protected]"
],
[
"delivered",
"deferred"
]
]Expected output
[
[
"[email protected]",
"delivered"
],
[
"[email protected]",
"deferred"
]
]The edge case
Input
[
[
"a",
"b"
],
[
1
]
]Expected output
[
[
"a",
1
],
[
"b",
null
]
]Know the limits
- Missing items become null (undefined in the original) so the result is JSON-representable.
- The original took arrays as separate arguments.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Zip multiple arrays from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Takes one array of arrays; missing items become null so the result is JSON-representable. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Clone an array — Create a new array without copying the objects inside it.
- Remove duplicate values — Keep the first occurrence of each value, in its original order.
- Flatten a nested array — Turn a nested array into one sequence of values.