Lodash _.zip() method
What you’ll learn
- How
_.zip(...arrays)merges aligned indices into row tuples. - Why this pairs naturally with
_.unzip()when reshaping tabular data. - When
zipWith, _.unzipWith(), or plainmapis clearer than raw tuples. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Picture each argument as a spreadsheet column—zip walks down the rows and bundles cells together.
- You are comfortable with nested arrays representing rows of cells.
- You can open Try-it labs or run snippets locally.
Overview
_.zip shines when parallel arrays describe the same entities—IDs beside labels beside flags—and you need row records for rendering tables, validation batches, or immutable updates.
Columns → rows
Index i of every argument becomes one tuple at output index i.
Non-destructive
Inputs stay untouched; tuples reference the same element objects as before.
Unzip symmetry
Pair it with unzip for reversible pivots when lengths line up.
Syntax
_.zip(...arrays) - arrays: two or more collections aligned by index—each acts like one column.
- Returns: array of tuples; row
ibundles indexifrom every argument (undefined where an argument is shorter).
Zip two parallel columns
Names and scores live in separate arrays—zip stitches each pair into one tuple.
import zip from "lodash/zip";
zip(["ada", "bob"], [100, 85]);
// → [["ada", 100], ["bob", 85]] Three-wide rows
Pass three aligned arrays to produce triples—useful before mapping rows into chart points or table rows.
import zip from "lodash/zip";
zip(
["north", "south", "east"],
[12, 8, 15],
[true, false, true]
);
// → [["north", 12, true], ["south", 8, false], ["east", 15, true]] Unequal lengths and round-trip
Shorter columns pad with undefined; combining zip with unzip restores rectangular columns when shapes match.
import zip from "lodash/zip";
import unzip from "lodash/unzip";
zip([1, 2, 3], ["a"]);
// → [[1, "a"], [2, undefined], [3, undefined]]
unzip(zip([1, 2], ["a", "b"]));
// → [[1, 2], ["a", "b"]] 📋 _.zip vs unzip, zipWith, unzipWith
| API | Direction | Use case |
|---|---|---|
_.zip(...arrays) | Columns → rows | Bundle aligned indices into tuples |
_.zipObject(props, values) | Columns → object | Pair key/value arrays into one plain object |
_.unzip(array) | Rows → columns | Split tuples back into parallel arrays |
_.zipWith(...arrays, iteratee) | Columns → reduced rows | Fuse slots with a combiner instead of inner arrays |
_.unzipWith(array, iteratee) | Rows → custom fold | Unzip then collapse each column immediately |
Pitfalls to avoid
Flatten confusion
zip nests tuples—reach for flatten or concat when you need one linear array.
undefined fillers
Mismatched lengths introduce holes—strip or default them before math or JSON APIs that reject undefined.
Shared references
Mutating objects pulled into tuples still affects the source arrays—clone when isolation matters.
❓ FAQ
Summary
- Purpose:
_.zip(...arrays)merges parallel arrays into tuples indexed by row. - Companion: reverse the pivot with _.unzip() when you already hold rows.
- Next: Lodash _.zipObject(), _.xorWith() (previous), or the array methods hub.
_.zip is the structural inverse of _.unzip()—think grouping spreadsheet columns into rows without mutating the originals.
6 people found this page helpful
