Lodash _.xorWith() method

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

What you’ll learn

  • How _.xorWith(...arrays, comparator) applies symmetric difference using your pairwise predicate.
  • Why comparators follow the same (arrVal, othVal) shape as _.unionWith().
  • When _.xorBy() or plain _.xor() stays simpler than custom compares.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.xorBy() first—if a single iteratee captures collisions, you rarely need the heavier comparator path.

  • You can implement symmetric compares without hidden mutable state.
  • You can open Try-it labs or run snippets locally.

Overview

_.xorWith covers XOR-style diffs where equality is fuzzy—deep payloads, composite keys, or domain-specific matching rules that would awkwardly squeeze into one iteratee.

Binary compares

You decide when two rows cancel during symmetric folding.

Shared XOR semantics

Conceptually still symmetric difference—matches disappear instead of merging.

Immutable inputs

Sources remain intact for alternate comparisons or auditing.

Syntax

javascript
_.xorWith(...arrays, comparator)
  • arrays: two or more collections participating in symmetric difference.
  • comparator: invoked as (arrVal, othVal); truthy means treat the pair as duplicates for cancellation.
  • Returns: new array of exclusive survivors under your comparator.
1

Rows matched by sku

Overlap on sku cancels both representatives—only exclusive SKUs remain.

javascript
import xorWith from "lodash/xorWith";

xorWith(
  [{ sku: "A1" }, { sku: "B2" }],
  [{ sku: "B2" }, { sku: "C3" }],
  (a, b) => a.sku === b.sku
);
// → [{ sku: "A1" }, { sku: "C3" }]
Try it Yourself
2

Deep equality with _.isEqual

Distinct references still cancel when Lodash deems structures deeply equivalent.

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

xorWith(
  [{ id: 1, meta: { z: 1 } }],
  [{ id: 2 }, { id: 1, meta: { z: 1 } }],
  isEqual
);
// → [{ id: 2 }]
Try it Yourself
3

Case-insensitive strings

Primitives compare fine—fold casing inside the comparator when lexeme identity should ignore case.

javascript
import xorWith from "lodash/xorWith";

xorWith(
  ["a", "B"],
  ["A", "c"],
  (x, y) => typeof x === "string" && typeof y === "string" && x.toLowerCase() === y.toLowerCase()
);
// → ["B", "c"]
Try it Yourself

📋 _.xorWith vs xor, xorBy, differenceWith

APIEquality signalUse case
_.xorWith(...arrays, comparator)Binary predicateSymmetric difference with custom pairwise logic
_.xor(...arrays)SameValueZeroPrimitives or reference-stable rows
_.xorBy(...arrays, iteratee)Projected keysSingle-field collisions
_.differenceWith(array, ...values, comparator)Binary predicateDirected subtraction from first array only

Pitfalls to avoid

Perf

Comparator cost

Deep compares magnify work on large arrays—measure hot paths.

Logic

Asymmetric predicates

Odd comparators that are not symmetric confuse XOR outcomes—keep rules predictable.

Dup

Duplicate toggling

Same as plain xor: repeated entries flip parity—normalize with uniq or uniqWith when you need set semantics.

❓ FAQ

No. Lodash returns a new array; originals stay unchanged.
Return a truthy value when the two arguments should count as the same element for XOR cancellation—symmetric predicates are easiest to reason about.
Always last: _.xorWith(...arrays, comparator).
xorBy projects each value to a scalar key; xorWith compares arbitrary pairs with your predicate—use it when equality is not a single-field projection.
Use import xorWith from "lodash/xorWith" or require("lodash/xorWith") for tree-shaken bundles.

Summary

  • Purpose: _.xorWith(...arrays, comparator) symmetric-differences arrays using your comparator for duplicate detection.
  • Contrast: prefer xorBy when a lone iteratee expresses collisions cleanly.
  • Next: Lodash _.zip(), _.xorBy() (previous), or the array methods hub.
Did you know?

Like _.unionWith(), the comparator answers “should these two slots cancel?”—truthy means treat them as duplicates for XOR folding.

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