Lodash _.differenceWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.difference, except it accepts a comparator invoked to compare elements of array to values. Called as (arrVal, othVal). Order and references come from the first array. Since Lodash 4.0.0. Unlike _.pullAllWith, it returns a new array.

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.differenceWith on an array. Start with this line.

Lodash
_.differenceWith(array, [values], [comparator])
CallWhat it does
_.differenceWith(array, [values], [comparator])Primary signature.
_.differenceWith(array)Call with an array argument.

Prefer import differenceWith from "lodash/differenceWith" when you want a smaller bundle.

Library declarations
Lodash
_.differenceWith(array, [values], [comparator])

Parameters

ParameterDescription
arrayRequired. Source to inspect
…valuesOptional. Arrays of values to exclude
comparatorOptional. (arrVal, othVal) ⇒ boolean (*omit ≈ plain difference)

Return Value

Return ValueDescription
*The value returned by _.differenceWith().

Lodash Examples

Example 1 — Official: Deep Equal With _.isEqual

Exclude objects that deeply equal an item in the other list.

Lodash
var objects = [{ x: 1, y: 2 }, { x: 2, y: 1 }];

_.differenceWith(objects, [{ x: 1, y: 2 }], _.isEqual);
// → [{ x: 2, y: 1 }]
Try It Yourself

How It Works

  1. Comparator. _.isEqual checks deep structural equality.
  2. First object. Matches { x: 1, y: 2 } → excluded.
  3. Second object. Different shape → kept.
  4. Result. New array with the surviving original object.

Example 2 — Custom Field Comparator

Match when both x and y are equal — ignore other fields.

Lodash
function sameXY(a, b) {
  return a.x === b.x && a.y === b.y;
}

_.differenceWith(
  [{ x: 1, y: 2, tag: "a" }, { x: 3, y: 4, tag: "b" }],
  [{ x: 1, y: 2 }],
  sameXY
);
// → [{ x: 3, y: 4, tag: "b" }]
Try It Yourself

How It Works

  1. Rule. Equal if x and y both match.
  2. First. tag differs but coordinates match → dropped.
  3. Second. Coordinates differ → kept whole object.
  4. Use. Partial equality when deep equal would be too strict.

Example 3 — Numeric Tolerance

Treat numbers within 0.1 as the same.

Lodash
function near(a, b) {
  return Math.abs(a - b) < 0.1;
}

_.differenceWith([1.0, 2.0, 3.0], [1.05, 4], near);
// → [2, 3]
Try It Yourself

How It Works

  1. Rule. Absolute difference < 0.1 → match.
  2. 1.0 vs 1.05. Close enough → exclude 1.
  3. 2 and 3. Far from excludes → kept.
  4. Use. Floating-point lists where exact equality fails.

Example 4 — differenceWith vs differenceBy

By projects one key; With decides equality with any rule.

Lodash
var a = [{ x: 1, y: 9 }, { x: 2, y: 8 }];
var b = [{ x: 1, y: 0 }];

_.differenceBy(a, b, "x");
// → [{ x: 2, y: 8 }]   // drops by x only

_.differenceWith(a, b, _.isEqual);
// → both kept            // y differs, not deep-equal
Try It Yourself

How It Works

  1. By. Same x: 1 → first object dropped.
  2. With + isEqual. Full objects differ → nothing dropped.
  3. Pick By when one field defines identity.
  4. Pick With when equality is richer than a single key.

Example 5 — Source Array Unchanged

Always returns a new array — originals stay intact.

Lodash
var objects = [{ x: 1 }, { x: 2 }];
var result = _.differenceWith(objects, [{ x: 1 }], _.isEqual);

result;   // [{ x: 2 }]
objects;  // [{ x: 1 }, { x: 2 }]
Try It Yourself

How It Works

  1. Filter. Build a new list of survivors.
  2. Source. objects still has both items.
  3. vs pullAllWith. That API mutates; this one does not.
  4. Safe. Prefer With when you must keep the original array.

Browser & Runtime Support

_.differenceWith is a Lodash 4.x Array helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.0+

_.differenceWith()

Available in [email protected] full builds and the lodash/differenceWith modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.differenceWith Lodash 4.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import differenceWith from "lodash/differenceWith" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

It is like _.difference, but you supply a comparator (arrVal, othVal) that returns true when two values should count as a match for exclusion. Survivors come from the first array in original order.
No. Unlike _.pullAllWith, it always returns a new array. Inputs are unchanged.
Return true when the two arguments should be treated as the same for removal. Return false when they are distinct. Symmetry is expected for predictable results.
Use differenceBy when one projected key or transform is enough (for example "id" or Math.floor). Use differenceWith for deep equality, tolerances, or multi-field rules.
Yes. _.differenceWith(objects, others, _.isEqual) is the common deep-equality pattern from the official docs.
Use import differenceWith from "lodash/differenceWith" or require("lodash/differenceWith") for tree-shaking. Full build: import _ from "lodash" then _.differenceWith.

Next: _.drop()

Create a slice of an array with n elements dropped from the start.

_.drop() tutorial →

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.

5 people found this page helpful