Lodash _.zipWith() method
What you’ll learn
- How
_.zipWith(...arrays, iteratee)fuses aligned slots with your combiner. - Why skipping the iteratee reproduces
_.zip()tuples. - How this differs from
_.unzipWith()when data starts as rows versus columns. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.zip() first—zipWith is the same alignment story with an optional reducer per index.
- You can write pure iteratees without depending on external mutable state.
- You can open Try-it labs or run snippets locally.
Overview
_.zipWith removes the intermediate tuple step when you already know how to merge each aligned slice—sums for ledger columns, string concat for labels, or library helpers like Math.max.
Per-index combine
Each row invokes your iteratee once with the gathered arguments.
Zip-compatible
Drop the iteratee and Lodash behaves like plain zip.
Immutable columns
Sources remain untouched for alternate combine strategies.
Syntax
_.zipWith(...arrays, [iteratee]) - arrays: two or more collections aligned by index.
- iteratee: optional; receives the aligned elements at each index and returns the combined output.
- Returns: new array with one entry per aligned row.
Add pairwise entries
Ledger-like columns reduce to running totals per row without allocating inner arrays.
import zipWith from "lodash/zipWith";
zipWith([1, 2], [10, 20], (a, b) => a + b);
// → [11, 22] Label each numeric slot
Use any deterministic combine—strings, objects, or helpers—as long as arity matches column count.
import zipWith from "lodash/zipWith";
zipWith(["score", "bonus"], [100, 25], (label, n) => `${label}:${n}`);
// → ["score:100", "bonus:25"] Math.max across three arrays
Lodash feeds each aligned triple into Math.max, producing row-wise maxima without nested loops.
import zipWith from "lodash/zipWith";
zipWith([1, 8], [4, 3], [2, 9], Math.max);
// → [4, 9] 📋 _.zipWith vs zip, unzipWith, zipObject
| API | Output | Use case |
|---|---|---|
_.zipWith(...arrays, iteratee) | Combined scalars/objects | Skip tuple allocation when you already know the reducer |
_.zip(...arrays) | Tuples | Keep arrays grouped per row |
_.unzipWith(array, iteratee) | Column aggregates | Start from zipped rows, pivot, then fold |
_.zipObject(props, values) | Plain object | Keys column plus values column |
Pitfalls to avoid
Iteratee signature drift
Adding another column changes argument positions—keep iteratees variadic or validate lengths first.
undefined gaps
Shorter arrays inject undefined into your iteratee—guard before numeric math.
Heavy closures
Very hot loops may prefer tight native loops; zipWith shines when readability wins.
❓ FAQ
Summary
- Purpose:
_.zipWith(...arrays, iteratee)merges aligned indices using your combiner. - Contrast: stay on zip when inner arrays should survive downstream.
- Next: Array method index, _.zipObjectDeep() (previous), or the array methods hub.
Iteratee placement matches other Lodash *With helpers—the combiner is always last after every array argument.
6 people found this page helpful
