Lodash _.intersectionWith() method

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

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) => boolean predicate.
  • 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

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

Deep equality with isEqual

Different object references with the same nested shape can still overlap when you pass Lodash isEqual as the comparator.

javascript
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 }]
Try it Yourself
2

Numeric tolerance

Treat two numbers as the same when their absolute difference is within a small epsilon.

javascript
import intersectionWith from "lodash/intersectionWith";

intersectionWith([1.0, 2.5, 3.1], [1.05], (a, b) => Math.abs(a - b) < 0.2);
// [1]
Try it Yourself
3

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.

javascript
import intersectionWith from "lodash/intersectionWith";

intersectionWith(
  [{ id: 1 }, { id: 2 }],
  [{ id: 1 }],
  [{ id: 1 }],
  (a, b) => a.id === b.id
);
// [{ id: 1 }]
Try it Yourself

📋 intersectionWith vs intersectionBy

MethodHow equality is decidedTypical use
_.intersectionBySameValueZero on iteratee(value)One stable key or projection per value
_.intersectionWithYour comparator on the raw pair (arrVal, othVal)Deep structures, tolerances, or multi-field logic

Pitfalls to avoid

Cost

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.

Sym

Comparator contract

Lodash invokes (candidateFromFirst, elementFromOther). Keep the predicate logically symmetric so the same pair always agrees.

Bool

Loose truthy returns

Prefer explicit booleans. Returning a stray number can accidentally read as a match.

❓ FAQ

No. Lodash returns a new array; each argument stays unchanged.
Return a truthy value when the two arguments should be treated as the same value for overlap checks (typically symmetric). Return falsy when they are distinct.
No. Lodash compares candidates from the first array against elements from every later argument using your comparator, similar to how differenceWith pairs against exclusions.
Use import intersectionWith from "lodash/intersectionWith" or require("lodash/intersectionWith") for tree-shaking friendly bundles.
Use intersectionBy when a single iteratee projection (property path or unary function) is enough. intersectionWith is for pairwise logic, tolerances, or deep compares.

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

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.

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