zip
Combines multiple arrays by position into an array of tuples. The inverse of unzip.
Try it
Section titled “Try it”Import
Section titled “Import”import { zip } from "1o1-utils";import { zip } from "1o1-utils/zip";Signature
Section titled “Signature”function zip<T>({ arrays, strategy }: ZipParams<T>): T[][]Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| arrays | T[][] | Yes | The arrays to combine |
| strategy | "fill" | "truncate" | No | How to handle uneven lengths. "fill" (default) pads shorter arrays with undefined; "truncate" cuts to the shortest. |
Returns
Section titled “Returns”T[][] — An array of tuples grouped by index.
Examples
Section titled “Examples”zip({ arrays: [["a", "b", "c"], [1, 2, 3]] });// => [["a", 1], ["b", 2], ["c", 3]]
zip({ arrays: [ ["a", "b"], [1, 2], [true, false], ],});// => [["a", 1, true], ["b", 2, false]]
zip({ arrays: [["a", "b", "c"], [1, 2]] });// => [["a", 1], ["b", 2], ["c", undefined]]
zip({ arrays: [["a", "b", "c"], [1, 2]], strategy: "truncate" });// => [["a", 1], ["b", 2]]Edge Cases
Section titled “Edge Cases”- Throws if
arraysis not an array. - Throws if any element of
arraysis not an array. - Throws if
strategyis not"fill"or"truncate". - Returns
[]whenarraysis empty.
Also known as
Section titled “Also known as”zip, pair, interleave, combine arrays, transpose
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 zip to pair two parallel arrays into tuples