Skip to content

unzip

Splits an array of tuples into separate arrays, one per position.

The inverse of zip.

Available since v2.0.0

import { unzip } from '@helpers4/array';
unzip<A, B>(pairs: readonly [A, B][] | null | undefined): [A[], B[]]
ParameterTypeDescription
pairsreadonly [A, B][] | null | undefinedArray of 2-tuples to unzip

[A[], B[]] — A tuple of two arrays: all first elements and all second elements

The inverse of zip — separate each position into its own array.

const pairs: [number, string][] = [[1, 'a'], [2, 'b'], [3, 'c']];
const [nums, letters] = unzip(pairs);

nums;    // => [1, 2, 3]
letters; // => ['a', 'b', 'c']

View source on GitHub