Lodash _.differenceBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.difference, except it accepts an iteratee invoked for each element of array and values to build the compare key. Order and references come from the first array. Since Lodash 4.0.0. Unlike _.pullAllBy, 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 _.differenceBy on an array. Start with this line.

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

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

Library declarations
Lodash
_.differenceBy(array, [values], [iteratee])

Parameters

ParameterDescription
arrayRequired. Source to inspect
…valuesOptional. Arrays whose projected values are excluded
iterateeOptional. Last arg; default identity

Return Value

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

Lodash Examples

Example 1 — Official: Compare After Math.floor

Decimals compare by their floor value.

Lodash
_.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// → [1.2]
Try It Yourself

How It Works

  1. Project. floor(2.1)=2, floor(1.2)=1.
  2. Deny. Excludes map to 2 and 3.
  3. Drop. 2.1 shares key 2 with 2.3.
  4. Keep. 1.2 (key 1) has no match — original kept.

Example 2 — Official: Property Shorthand

A string iteratee reads that property on each object.

Lodash
_.differenceBy(
  [{ x: 2 }, { x: 1 }],
  [{ x: 1 }],
  "x"
);
// → [{ x: 2 }]
Try It Yourself

How It Works

  1. Shorthand. "x" means compare obj.x.
  2. Deny. Exclusion has x: 1.
  3. Drop. Source object with x: 1 is excluded.
  4. Keep. Object with x: 2 remains (by reference from source).

Example 3 — Exclude Users by id

Common pattern: remove objects that share an id with another list.

Lodash
_.differenceBy(
  [{ id: 1, name: "Ada" }, { id: 2, name: "Lin" }],
  [{ id: 2 }],
  "id"
);
// → [{ id: 1, name: "Ada" }]
Try It Yourself

How It Works

  1. Key. Compare on id only.
  2. Deny. Exclusion list contains id 2.
  3. Drop Lin. Matching id → removed from the result.
  4. Keep Ada. Full original object is returned.

Example 4 — Custom Iteratee Function

Any function of one argument works as the compare key.

Lodash
_.differenceBy(
  ["apple", "pear", "plum"],
  ["peach"],
  function (s) { return s[0]; }
);
// → ["apple"]
Try It Yourself

How It Works

  1. Key. First character of each string.
  2. Deny. "peach" → key "p".
  3. Drop. "pear" and "plum" also start with p.
  4. Keep. Only "apple" (key "a") survives.

Example 5 — Why Not Plain _.difference?

Without an iteratee, objects compare by reference — property shorthand fixes that.

Lodash
var a = [{ x: 1 }, { x: 2 }];
var b = [{ x: 1 }];

_.difference(a, b);           // both objects kept (different refs)
_.differenceBy(a, b, "x");    // [{ x: 2 }]
Try It Yourself

How It Works

  1. difference. Objects are different references → nothing excluded.
  2. differenceBy. Compare x → x: 1 matches and drops.
  3. Lesson. Use By when “same shape” matters more than “same instance.”
  4. Next. Need a custom equality test? Use _.differenceWith.

Browser & Runtime Support

_.differenceBy 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+

_.differenceBy()

Available in [email protected] full builds and the lodash/differenceBy 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
_.differenceBy Lodash 4.0+

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

Frequently Asked Questions

It is like _.difference, but each element is passed through an iteratee first. Lodash compares the projected values with SameValueZero, then returns matching originals from the first array that are not excluded.
No. Unlike _.pullAllBy, it always returns a new array. Inputs are unchanged.
Yes. The iteratee runs on elements of the first array and on every flattened element from the exclusion arrays before comparison.
A missing iteratee behaves like identity — same idea as _.difference for the same array arguments.
Use differenceWith when you need a custom comparator (for example deep equality or a tolerance). Use differenceBy when one projected key or transform is enough.
Use import differenceBy from "lodash/differenceBy" or require("lodash/differenceBy") for tree-shaking. Full build: import _ from "lodash" then _.differenceBy.

Next: _.differenceWith()

Compare elements with your own comparator function.

_.differenceWith() 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.

6 people found this page helpful