Lodash _.intersectionWith() method
What you’ll learn
- How
_.intersectionWith(...arrays, comparator)keeps elements from the first array that comparator-match at least one element in each of the other arrays. - Using
isEqual, numeric tolerances, or your own(arrVal, othVal) => booleanpredicate. - How this differs from _.intersectionBy() (single projection) and from _.differenceWith() (subtraction instead of overlap).
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.intersectionBy() and _.differenceWith(); you should be comfortable passing small comparator functions to library APIs.
- You understand that intersection means “appears in every list,” not merely in any list.
- You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.intersectionWith is the pairwise twin of _.intersectionBy: you supply a comparator that decides whether two elements “count as the same” for membership. Deep structures, floating tolerances, and multi-field rules all live here.
Comparator last
Pass every array first, then the comparator function, mirroring other *With helpers.
All lists
A first-array element survives only if your comparator finds a partner in each additional array.
Non-destructive
Nothing is mutated; you always get a fresh array of references from the first list.
Syntax
_.intersectionWith(...arrays, comparator) - arrays: two or more collections; the first supplies candidate elements and ordering.
- comparator: final argument:
(arrVal, othVal) => boolean— truthy means treat the pair as equal for overlap. - Returns: a new array of first-array elements that match at least one element in every other array under your comparator.
Deep equality with isEqual
Different object references with the same nested shape can still overlap when you pass Lodash isEqual as the comparator.
import intersectionWith from "lodash/intersectionWith";
import isEqual from "lodash/isEqual";
intersectionWith(
[{ x: 1, y: 2 }, { x: 2, y: 1 }],
[{ x: 1, y: 2 }],
isEqual
);
// [{ x: 1, y: 2 }] Numeric tolerance
Treat two numbers as the same when their absolute difference is within a small epsilon.
import intersectionWith from "lodash/intersectionWith";
intersectionWith([1.0, 2.5, 3.1], [1.05], (a, b) => Math.abs(a - b) < 0.2);
// [1] Three lists and an id comparator
Each additional array must contain some element that your comparator considers equal to the candidate from the first array.
import intersectionWith from "lodash/intersectionWith";
intersectionWith(
[{ id: 1 }, { id: 2 }],
[{ id: 1 }],
[{ id: 1 }],
(a, b) => a.id === b.id
);
// [{ id: 1 }] 📋 intersectionWith vs intersectionBy
| Method | How equality is decided | Typical use |
|---|---|---|
_.intersectionBy | SameValueZero on iteratee(value) | One stable key or projection per value |
_.intersectionWith | Your comparator on the raw pair (arrVal, othVal) | Deep structures, tolerances, or multi-field logic |
Pitfalls to avoid
Many pairwise checks
Each candidate is checked against elements in every other list. Very large inputs may need a map or ID normalization instead of an expensive comparator.
Comparator contract
Lodash invokes (candidateFromFirst, elementFromOther). Keep the predicate logically symmetric so the same pair always agrees.
Loose truthy returns
Prefer explicit booleans. Returning a stray number can accidentally read as a match.
❓ FAQ
Summary
- Purpose:
_.intersectionWith(...arrays, comparator)returns first-array elements that comparator-match something in every other array. - Safety: inputs are not mutated.
- Next: Lodash _.join(), _.intersectionBy() (earlier in this track), or the array methods hub.
Return true when the pair should count as equal for overlap. The same comparator idea mirrors _.differenceWith(), but intersection keeps what matches every list instead of subtracting exclusions.
6 people found this page helpful
