Lodash _.intersectionBy() method
What you’ll learn
- How
_.intersectionBy(...arrays, iteratee)keeps values from the first array whose projected key appears in every other list. - Using a function, a property path string, or other Lodash iteratee shorthand (same rules as _.differenceBy()).
- How this extends plain _.intersection() and when to prefer _.intersectionWith() for custom comparators.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.intersection() first; optional skim of Lodash iteratee docs (functions vs property paths).
- You understand that two objects with different references but the same
idfield can still “match” after projection. - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.intersectionBy is the keyed twin of _.intersection: instead of comparing raw elements, Lodash compares whatever your iteratee returns for each value. That makes joins on IDs, ranks, or grouped numbers straightforward.
Project first
Same iteratee runs on the first array and on every later array before SameValueZero checks.
Order preserved
Like plain intersection, uniqueness and ordering follow the first array’s surviving elements.
Non-destructive
All inputs stay read-only; you receive a new array of references from the first list.
Syntax
_.intersectionBy(...arrays, iteratee) - arrays: two or more arrays (or array-like values) whose elements are mapped before comparison.
- iteratee: final argument: function, property path string, or other Lodash iteratee shorthand.
- Returns: a new array of elements from the first array whose projected value appears in every other list (SameValueZero on projections).
Numeric projection with Math.floor
Decimals that land on the same integer after Math.floor count as the same key for overlap.
import intersectionBy from "lodash/intersectionBy";
intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// [2.1] Property path shorthand
Compare objects by a field name; the first array’s element wins when multiple share the same key.
import intersectionBy from "lodash/intersectionBy";
intersectionBy([{ x: 1 }], [{ x: 2 }, { x: 1 }], "x");
// [{ x: 1 }] Three lists and one iteratee
Every extra array must also contain a projected value that matches the first array’s candidate.
import intersectionBy from "lodash/intersectionBy";
intersectionBy([{ id: 1, n: "a" }], [{ id: 1, n: "b" }], [{ id: 1, n: "c" }], "id");
// [{ id: 1, n: "a" }] 📋 _.intersectionBy vs _.intersection
| API | Comparison | Use when |
|---|---|---|
_.intersection(...arrays) | SameValueZero on raw elements | Primitives or intentional reference identity |
_.intersectionBy(...arrays, iteratee) | SameValueZero on iteratee(item) (or path) | Objects should match by id, rank, rounded value, etc. |
Pitfalls to avoid
Iteratee must be last
Pass every array first, then the iteratee. Swapping order silently changes which argument Lodash treats as the iteratee.
Missing keys
A missing path often yields undefined; many lists then “agree” on undefined unintentionally. Normalize data first if needed.
Not a comparator
intersectionBy projects to one value per element. Pairwise tolerance checks belong in intersectionWith.
❓ FAQ
Summary
- Purpose:
_.intersectionBy(...arrays, iteratee)returns elements from the first array whose projected value appears in every other array. - Equality: SameValueZero applies to projected values, not necessarily to whole objects.
- Next: Lodash _.intersectionWith(), _.intersection() (earlier in this track), or the array methods hub.
The last argument is Lodash’s iteratee: a string like "id" behaves like _.property("id"), consistent with _.differenceBy() and _.map.
6 people found this page helpful
