Lodash _.unzip() method
What you’ll learn
- How
_.unzip(array)pivots row-wise tuples into column-wise arrays. - Why this helper pairs naturally with
_.zip()when reshaping tabular data. - When
zipWith, _.unzipWith(), or manualmapcolumns deserve a different tool. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Comfort nesting arrays ([[a, b], [c, d]]) keeps the mental model crisp—each inner array is one logical row before the pivot.
- You can picture matrices as rows of cells read left-to-right.
- You can open Try-it labs or run snippets locally.
Overview
_.unzip helps when APIs deliver zipped tuples—coordinates paired as rows, CSV chunks as arrays—and you need independent arrays per column for charting, validation, or broadcasting.
Pivot rows → cols
Each tuple index becomes its own output array preserving encounter order.
Non-destructive shell
Outer arrays are new; inner element references stay tied to the source tuples.
Zip symmetry
Think of unzip as undoing zip when both sides use consistent rectangular data.
Syntax
_.unzip(array) - array: grouped collection—typically an array of tuples produced by hand or by
zip. - Returns: array of columns; column
icollects theith element from every tuple.
Split paired tuples into two columns
Names and scores arrive as rows—unzip separates them into parallel arrays.
import unzip from "lodash/unzip";
unzip([
["ada", 100],
["bob", 85]
]);
// → [["ada", "bob"], [100, 85]] Three-wide tuples
Wider CSV-style rows fan out into three aligned arrays—great before passing columns into chart constructors.
import unzip from "lodash/unzip";
unzip([
["north", 12, true],
["south", 8, false],
["east", 15, true]
]);
// → [["north", "south", "east"], [12, 8, 15], [true, false, true]] Round-trip with _.zip
Zip merges standalone arrays into tuples; unzip restores the originals when lengths stay aligned.
import unzip from "lodash/unzip";
import zip from "lodash/zip";
unzip(zip([1, 2], ["a", "b"]));
// → [[1, 2], ["a", "b"]] 📋 _.unzip vs zip, zipWith, unzipWith
| API | Direction | Use case |
|---|---|---|
_.unzip(array) | Rows → columns | Pivot zipped tuples without touching elements |
_.zip(...arrays) | Columns → rows | Interleave parallel arrays into tuples |
_.zipWith(...arrays, iteratee) | Columns → rows | Combine tuple slots with a reducer instead of keeping arrays |
_.unzipWith(array, iteratee) | Rows → custom fold | Unzip then immediately collapse each column via iteratee |
Pitfalls to avoid
Flatten confusion
unzip preserves tuple boundaries—reach for flatten or native flatMap when you need one contiguous array.
Uneven rows
Shorter tuples introduce undefined placeholders—sanitize before numeric math or serialization guards.
Shared references
Mutating objects inside an output column mutates the originals—clone first when isolation matters.
❓ FAQ
Summary
- Purpose:
_.unzip(array)splits row tuples into parallel column arrays. - Companion: pair it mentally with _.zip() for reversible reshaping.
- Next: Lodash _.unzipWith(), _.uniqWith() (previous), or the array methods hub.
_.unzip is the structural inverse of _.zip()—think pivoting spreadsheet rows into columns without mutating the originals.
6 people found this page helpful
