Lodash _.differenceWith() method

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

What you’ll learn

  • How _.differenceWith(array, ...values, comparator) drops items when your comparator says they match any exclusion value.
  • Pairing differenceWith with _.isEqual for deep structural compares.
  • When to stay with _.differenceBy for a single projection versus a full comparator.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.difference() and _.differenceBy() on this site first.

  • You can write a pure predicate (a, b) => boolean without mutating a or b.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.differenceWith filters the first array like difference, but two values count as the same whenever your comparator returns truthy. That unlocks deep equality, numeric tolerances, or multi-field rules that do not fit a single iteratee projection.

Deep equality

Pass _.isEqual (or similar) so different object instances with the same shape are excluded.

Custom rules

Encode tolerances, case-insensitive strings, or domain-specific “same record” logic in one function.

Non-destructive

Inputs are read-only; the result is a new array of references from the first array.

Syntax

javascript
_.differenceWith(array, [values], comparator)
  • array: source collection to filter.
  • values: zero or more arrays (each flattened one level) whose elements act as exclusion candidates.
  • comparator: (arrVal, othVal) => boolean — return a truthy value when the two should be treated as equal for removal.
  • Returns: a new array of elements from array that do not comparator-match any flattened exclusion value.
1

Deep equality with isEqual

Different object references with the same nested shape are removed when you pass Lodash isEqual as the comparator.

javascript
import differenceWith from "lodash/differenceWith";
import isEqual from "lodash/isEqual";

differenceWith(
  [{ x: 1, y: 2 }, { x: 2, y: 1 }],
  [{ x: 1, y: 2 }],
  isEqual
);
// [{ x: 2, y: 1 }]
Try it Yourself
2

Numeric tolerance

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

javascript
import differenceWith from "lodash/differenceWith";

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

Several exclusion lists

Later arrays are flattened one level and unioned; the same comparator applies to every pair check.

javascript
import differenceWith from "lodash/differenceWith";

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

📋 differenceWith vs differenceBy

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

Pitfalls to avoid

Cost

Quadratic comparisons

Each survivor is checked against every exclusion value. Huge lists may need hashing or mapping to IDs first instead of a heavy comparator.

Contract

Asymmetric comparators

Lodash will call your function with (candidate, exclusion) in that order; keep the predicate consistent so the same logical pair always matches regardless of argument order if you reuse the function elsewhere.

Truthiness

Loose truthy returns

Return a strict boolean when possible. Accidentally returning a non-zero number can look like a match and hide bugs.

❓ FAQ

No. Lodash returns a new array; inputs are unchanged.
Return true when the two arguments should count as the same value for exclusion (typically symmetry is expected). Return false when they are distinct.
No. Lodash compares elements from the first array against flattened elements from every later argument, using your comparator instead of SameValueZero.
Use import differenceWith from "lodash/differenceWith" or require("lodash/differenceWith") for tree-shaking friendly bundles.
Omitting it or passing a non-function falls back to the same comparison path as _.difference for the remaining arguments.

Summary

  • Purpose: _.differenceWith(array, ...values, comparator) filters the first array using your comparator instead of reference or SameValueZero checks alone.
  • Safety: inputs are not mutated.
  • Next: Lodash _.drop(), _.drop(), or the array methods hub.
Did you know?

Return true from the comparator when the two values should be treated as a match for removal. Lodash pairs each candidate from the first array against exclusion values using that predicate; order of the first array is preserved among survivors.

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