Lodash _.xorBy() method

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

What you’ll learn

  • How _.xorBy(...arrays, iteratee) keeps entries whose projected keys are exclusive.
  • Why iteratee placement and projection consistency matter for correctness.
  • When _.xor() or comparator-based xorWith is the better fit.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Understand _.xor() first; xorBy keeps the same symmetric-difference idea but compares iteratee outputs instead of whole values.

  • You can describe a stable projection key such as id or Math.floor(value).
  • You can open Try-it labs or run snippets locally.

Overview

_.xorBy is useful when payloads differ structurally but collide on business keys—inventory by SKU, records by id, or numeric buckets by floor/round projections.

Syntax

javascript
_.xorBy(...arrays, iteratee)
  • arrays: two or more collections to compare symmetrically.
  • iteratee: function/string/path used to project a comparison key for each element.
  • Returns: new array of elements whose projected keys are exclusive after xor folding.
1

Objects by id

Shared ids cancel even if object payload fields differ.

javascript
import xorBy from "lodash/xorBy";

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
2

Numbers by Math.floor

Buckets that appear on both sides cancel; exclusive buckets survive.

javascript
import xorBy from "lodash/xorBy";

xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// → [1.2, 3.4]
Try it Yourself
3

Three arrays, one iteratee

XOR folding continues across each additional array, still keyed by iteratee output.

javascript
import xorBy from "lodash/xorBy";

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

📋 _.xorBy vs xor, xorWith, differenceBy

APIEquality signalBest when
_.xorBy(...arrays, iteratee)Projected keyExclusive merge on id/path/math buckets
_.xor(...arrays)Whole value SameValueZeroPrimitive arrays or stable references
_.xorWith(...arrays, comparator)ComparatorPairwise deep/custom equality
_.differenceBy(array, ...values)Projected keyDirectional subtraction from first array only

Pitfalls to avoid

Pos

Iteratee placement

The iteratee must be the final argument, after all arrays.

Key

Unstable projections

Random/non-deterministic keys break xor logic; use stable fields or pure transforms.

Cmp

Need comparator, not key

If equality needs pairwise logic, use xorWith.

❓ FAQ

No. Lodash returns a new array; source arrays remain unchanged.
Always last: _.xorBy(...arrays, iteratee).
Lodash compares iteratee outputs using SameValueZero, then keeps values whose keys survive symmetric-difference folding.
xor compares whole values; xorBy compares projected keys (such as id, path, or Math.floor outputs).
Use import xorBy from "lodash/xorBy" or require("lodash/xorBy").

Summary

Did you know?

Think of xorBy as _.xor() on projected keys—great when whole objects differ but a field like id should decide exclusivity.

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