Lodash _.unzipWith() method
What you’ll learn
- How
_.unzipWith(array, iteratee)pivots tuples then collapses each column in one call chain. - Why iteratees receive a spread of values sourced from every row at the same column index.
- When plain _.unzip() (keep arrays) or manual
mapbeats a fused helper. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Understand _.unzip() first—unzipWith runs the same pivot, then replaces each column array with your reducer output.
- You can read variadic functions such as
(...vals) => vals.reduce(fn). - You can open Try-it labs or run snippets locally.
Overview
_.unzipWith shines when telemetry batches arrive zipped yet analytics needs scalar aggregates per dimension—sums, maxima, joins—without an intermediate map pass.
Pivot + fold
One helper expresses both regrouping and per-column reduction.
Variadic iteratees
Column arity mirrors tuple width—wider rows mean more parameters.
Immutable shell
Inputs remain untouched while you emit fresh reduced columns.
Syntax
_.unzipWith(array, [iteratee]) - array: tuples to pivot—same expectations as
_.unzip. - iteratee (optional): invoked per column with spread arguments from each row at that index.
- Returns: array whose length equals tuple width; entries are iteratee outputs (or inner arrays when iteratee omitted).
Sum each column with _.add
Zipped numeric rows collapse into totals per dimension—ideal for quick rollup previews.
import unzipWith from "lodash/unzipWith";
import add from "lodash/add";
unzipWith(
[
[1, 10, 100],
[2, 20, 200]
],
add
);
// → [3, 30, 300] Maximum per column
Spread arguments into Math.max when you need winners across aligned readings.
import unzipWith from "lodash/unzipWith";
unzipWith(
[
[1, 50],
[9, 12],
[4, 77]
],
(...vals) => Math.max(...vals)
);
// → [9, 77] Merge string columns
Pairwise tokens become deterministic labels—iteratees are not limited to math helpers.
import unzipWith from "lodash/unzipWith";
unzipWith(
[
["a", "x"],
["b", "y"]
],
(left, right) => left + ":" + right
);
// → ["a:b", "x:y"] 📋 _.unzipWith vs unzip, zipWith, manual map
| API | Output per column | Best when |
|---|---|---|
_.unzipWith(array, iteratee) | Reduced scalar/object | Pivot plus aggregate in one shot |
_.unzip(array) | Array of column values | You still need independent arrays for plotting pipelines |
_.zipWith(...arrays, iteratee) | Row tuples via forward zip | Sources start as parallel arrays, not rows |
unzip then map(columnFn) | Custom per step | Debugging intermediate pivots or mixing iterators |
Pitfalls to avoid
undefined slots
Uneven tuples flow through as missing arguments—guard reduce logic before calling strict numeric APIs.
Tuple width drift
Changing column counts changes iteratee arity overnight—keep schemas stable or branch inside the reducer.
Heavy iteratees
Expensive reducers run once per column but still allocate spreads—profile huge batches.
❓ FAQ
Summary
- Purpose:
_.unzipWith(array, iteratee)pivots tuples then folds each column via your iteratee. - Fallback: skip the iteratee when you only need raw columns—plain unzip stays clearer.
- Next: Lodash _.without(), _.unzip() (previous), or the array methods hub.
Treat unzipWith as _.unzip() followed by a per-column reducer—mirroring how zipWith inserts an iteratee into the forward zip path.
6 people found this page helpful
