Lodash _.intersectionBy() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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 id field 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

javascript
_.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).
1

Numeric projection with Math.floor

Decimals that land on the same integer after Math.floor count as the same key for overlap.

javascript
import intersectionBy from "lodash/intersectionBy";

intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// [2.1]
Try it Yourself
2

Property path shorthand

Compare objects by a field name; the first array’s element wins when multiple share the same key.

javascript
import intersectionBy from "lodash/intersectionBy";

intersectionBy([{ x: 1 }], [{ x: 2 }, { x: 1 }], "x");
// [{ x: 1 }]
Try it Yourself
3

Three lists and one iteratee

Every extra array must also contain a projected value that matches the first array’s candidate.

javascript
import intersectionBy from "lodash/intersectionBy";

intersectionBy([{ id: 1, n: "a" }], [{ id: 1, n: "b" }], [{ id: 1, n: "c" }], "id");
// [{ id: 1, n: "a" }]
Try it Yourself

📋 _.intersectionBy vs _.intersection

APIComparisonUse when
_.intersection(...arrays)SameValueZero on raw elementsPrimitives 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

Last

Iteratee must be last

Pass every array first, then the iteratee. Swapping order silently changes which argument Lodash treats as the iteratee.

Path

Missing keys

A missing path often yields undefined; many lists then “agree” on undefined unintentionally. Normalize data first if needed.

Cmp

Not a comparator

intersectionBy projects to one value per element. Pairwise tolerance checks belong in intersectionWith.

❓ FAQ

No. Lodash returns a new array of elements from the first array whose projected value appears in every other list; inputs stay unchanged.
Yes. Each element from the first array and each element from the other arrays is mapped through the same iteratee before SameValueZero comparison.
It is always the final argument after all arrays, matching _.differenceBy and other *By helpers.
Use import intersectionBy from "lodash/intersectionBy" or require("lodash/intersectionBy") for tree-shaking friendly bundles.
Use intersectionWith when equality should be decided by a comparator (for example tolerances) rather than by a single projected value per element.

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.
Did you know?

The last argument is Lodash’s iteratee: a string like "id" behaves like _.property("id"), consistent with _.differenceBy() and _.map.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful