Skip to main content

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"]