unzip
Splits an array of grouped tuples back into separate arrays. The inverse of zip.
Try it
Section titled “Try it”Import
Section titled “Import”import { unzip } from "1o1-utils";import { unzip } from "1o1-utils/unzip";Signature
Section titled “Signature”function unzip<T>({ array, strategy }: UnzipParams<T>): T[][]Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| array | T[][] | Yes | The array of tuples to ungroup |
| strategy | "fill" | "truncate" | No | How to handle uneven inner lengths. "fill" (default) pads shorter tuples with undefined; "truncate" cuts to shortest. |
Returns
Section titled “Returns”T[][] — An array of arrays grouped by tuple position.
Examples
Section titled “Examples”unzip({ array: [["a", 1], ["b", 2], ["c", 3]] });// => [["a", "b", "c"], [1, 2, 3]]
unzip({ array: [["a", 1], ["b"]] });// => [["a", "b"], [1, undefined]]
unzip({ array: [ ["a", 1, true], ["b", 2], ], strategy: "truncate",});// => [["a", "b"], [1, 2]]Edge Cases
Section titled “Edge Cases”- Throws if
arrayis not an array. - Throws if any element of
arrayis not an array. - Throws if
strategyis not"fill"or"truncate". - Returns
[]whenarrayis empty.
Also known as
Section titled “Also known as”unzip, transpose, ungroup, split tuples
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use unzip to split an array of [key, value] tuples into two arrays