Lodash _.xorBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.xor, but each element is compared by an iteratee result (function, property, or path). Shared projected keys cancel; exclusive keys keep their original elements. Part of Lodash 4.x Array.

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

Syntax

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

Lodash
_.xorBy([arrays], [iteratee=_.identity])
CallWhat it does
_.xorBy([arrays], [iteratee=_.identity])Primary signature.
_.xorBy(array)Call with an array argument.

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

Library declarations
Lodash
_.xorBy([arrays], [iteratee=_.identity])

Parameters

ParameterDescription
arraysRequired. Collections to compare
iterateeOptional. Key projector; default _.identity

Return Value

Return ValueDescription
*New array of exclusive elements

Lodash Examples

Example 1 — Official: Math.floor

Numeric buckets cancel when their floors match.

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

How It Works

  1. Project. floor(2.1)=2, floor(1.2)=1, floor(2.3)=2, floor(3.4)=3.
  2. Shared key. Floor 2 appears in both → those elements cancel.
  3. Exclusive. Floors 1 and 3 survive → 1.2 and 3.4.
  4. Keep values. Lodash returns the original numbers, not the floors.

Example 2 — Official: Property Shorthand

Compare objects by a field name string.

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

How It Works

  1. Shorthand. "x" means read each object’s x property.
  2. Cancel. Key 1 appears in both arrays → both { x: 1 } drop.
  3. Keep. Key 2 is only on the right → { x: 2 } remains.
  4. Tip. Same idea as _.property("x").

Example 3 — Objects by id

Shared ids cancel even when other fields differ.

Lodash
_.xorBy(
  [{ id: 1, name: "a" }, { id: 2, name: "b" }],
  [{ id: 2, extra: true }, { id: 3, name: "c" }],
  "id"
);
// → [{ id: 1, name: "a" }, { id: 3, name: "c" }]
Try It Yourself

How It Works

  1. Keys. Compare by id: 1, 2 vs 2, 3.
  2. Shared. id: 2 on both sides → both objects with that id cancel.
  3. Exclusive. id: 1 and id: 3 survive with their original payloads.
  4. Use. Great for records that differ in shape but share a business key.

Example 4 — Three Arrays, One Iteratee

XOR still folds left to right; the iteratee stays last.

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

How It Works

  1. Step 1. xorBy first two arrays by x → [{x:1},{x:3}].
  2. Step 2. Fold with third → key 3 cancels.
  3. Keep. Keys 1 and 4 remain.
  4. Placement. "x" must stay the final argument after all arrays.

Example 5 — Same Keys Cancel Completely

When every projected key overlaps, the result is empty.

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

How It Works

  1. Keys. Both sides have id 1 and 2.
  2. Cancel. Every key pairs off → nothing exclusive left.
  3. Extra fields. tag does not matter — only the iteratee key does.
  4. Safe. Returns []; does not throw.

Browser & Runtime Support

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

✓ Stable · Lodash 4.x

_.xorBy()

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

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import xorBy from "lodash/xorBy" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.xorBy(...arrays, iteratee) is like _.xor, but it compares each element by the value returned from iteratee. Shared projected keys cancel; exclusive keys keep their original elements.
No. It returns a new array. All argument arrays stay unchanged.
Last. Pass every array first, then the iteratee: _.xorBy(a, b, Math.floor) or _.xorBy(a, b, "id").
xor compares whole values with SameValueZero. xorBy compares projected keys (floor buckets, object fields, custom functions).
When you need a pairwise comparator (two arguments) instead of a single projected key. xorBy is for keys; xorWith is for custom equality.
Use import xorBy from "lodash/xorBy" or require("lodash/xorBy") for tree-shaking. Full build: import _ from "lodash" then _.xorBy.

Next: _.xorWith()

Run symmetric difference with a custom pairwise comparator.

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

10 people found this page helpful