Aller au contenu principal

unzip()

unzip<T>(tuples): UnzipResult<T>

Transposes an array of tuples into separate arrays grouped by index position.

Performs the inverse of zip, separating combined tuples back into their constituent arrays.


Type Parameters

T: T extends readonly unknown[]

A tuple type representing the structure of each element.


Parameters

tuples: readonly T[]

The array of tuples to unzip.


Returns: UnzipResult<T>

An array of arrays where result[i] contains all values from position i of each tuple.


Throws

RangeError If tuples have inconsistent lengths.


See Also

zip


Since

2.0.0


Performance

O(n × m) time & space where n = tuple length, m = number of tuples. Pre-allocates result arrays with new Array(n) for minimal allocation overhead.


Also known as

transpose (Ramda) · unzip (Lodash, es-toolkit, Remeda, Radashi, Effect) · ❌ (Modern Dash, Antfu)


Example

unzip([['a', 1], ['b', 2], ['c', 3]] as const);
// => [['a', 'b', 'c'], [1, 2, 3]]
// Type: [string[], number[]]

unzip([[1, true, 'x'], [2, false, 'y']] as const);
// => [[1, 2], [true, false], ['x', 'y']]
// Type: [number[], boolean[], string[]]

How it works?

Transposes an array of tuples — the inverse of zip. Separates combined tuples back into their constituent arrays.

zip ↔ unzip


Use Cases

Separate coordinates into individual axis arrays 📌

Transform point data into separate X, Y arrays. Perfect for data visualization, charting libraries, or graphics.

const points: [number, number][] = [
[0, 10],
[1, 25],
[2, 18],
[3, 42],
];

const [xValues, yValues] = unzip(points);
// xValues => [0, 1, 2, 3]
// yValues => [10, 25, 18, 42]

Parse row data into column arrays

Convert row-based data into columnar format for analysis. Ideal for spreadsheet processing or database imports.

const salesData: [string, string, number][] = [
["2024-01-15", "Widget A", 150],
["2024-01-16", "Widget B", 89],
["2024-01-17", "Widget A", 200],
];

const [dates, products, quantities] = unzip(salesData);
// dates => ["2024-01-15", "2024-01-16", "2024-01-17"]
// products => ["Widget A", "Widget B", "Widget A"]
// quantities => [150, 89, 200]

Decompose key-value pairs for batch operations

Separate keys and values from entries for parallel processing. Useful for API requests or config management.

const settings: [string, string | number][] = [
["theme", "dark"],
["fontSize", 14],
["language", "en-US"],
];

const [keys, values] = unzip(settings);
// keys => ["theme", "fontSize", "language"]
// values => ["dark", 14, "en-US"]