Unzip an array of arrays
Regroup the n-th item of every array into the n-th output array.
function unzip(arrays) { return Array.from({ length: Math.max(0, ...arrays.map(array => array.length)) }, (_, i) => arrays.filter(array => i < array.length).map(array => array[i])); }
Input & expected output
Input
[
[
"[email protected]",
"delivered"
],
[
"[email protected]",
"deferred"
]
]Expected output
[
[
"[email protected]",
"[email protected]"
],
[
"delivered",
"deferred"
]
]The edge case
Input
[]Expected output
[]Know the limits
- Shorter arrays simply contribute fewer items.
- The inverse of Zip multiple arrays.
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 Unzip an array of arrays from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Builds each column with filter and map instead of pushing into accumulators; handles an empty input. 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.