Lodash _.xorWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.xor, but equality is decided by a comparator invoked as (arrVal, othVal). Truthy means the pair matches and cancels; exclusive values stay. 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 _.xorWith on an array. Start with this line.

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

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

Library declarations
Lodash
_.xorWith([arrays], [comparator])

Parameters

ParameterDescription
arraysRequired. Collections to compare
comparatorOptional. Pairwise equality; default identity-style

Return Value

Return ValueDescription
*New array of exclusive elements

Lodash Examples

Example 1 — Official: Deep Equality

Use _.isEqual so look-alike objects cancel across arrays.

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

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

How It Works

  1. Match. { x: 1, y: 2 } appears in both (deep equal) → cancel.
  2. Left only. { x: 2, y: 1 } has no match → keep.
  3. Right only. { x: 1, y: 1 } has no match → keep.
  4. vs _.xor. Plain xor would keep all four — different references never cancel.

Example 2 — Match by Field

Cancel rows when a business field matches, even if other fields differ.

Lodash
_.xorWith(
  [{ sku: "A1" }, { sku: "B2" }],
  [{ sku: "B2" }, { sku: "C3" }],
  function (a, b) { return a.sku === b.sku; }
);
// → [{ sku: "A1" }, { sku: "C3" }]
Try It Yourself

How It Works

  1. Rule. Two rows match when sku is equal.
  2. Shared. B2 appears on both sides → cancel.
  3. Exclusive. A1 and C3 survive.
  4. Note. For a single field, _.xorBy(..., "sku") is often simpler.

Example 3 — Case-Insensitive Strings

Fold casing inside the comparator so "a" cancels "A".

Lodash
_.xorWith(
  ["a", "B"],
  ["A", "c"],
  function (x, y) {
    return String(x).toLowerCase() === String(y).toLowerCase();
  }
);
// → ["B", "c"]
Try It Yourself

How It Works

  1. Match. "a" and "A" compare equal after lowercasing → cancel.
  2. Keep left. "B" has no case-insensitive partner.
  3. Keep right. "c" is exclusive on the second array.
  4. Survivors. Original casing is preserved in the result.

Example 4 — Nested Objects with _.isEqual

Deep structures cancel even when they are different object instances.

Lodash
_.xorWith(
  [{ id: 1, meta: { z: 1 } }],
  [{ id: 2 }, { id: 1, meta: { z: 1 } }],
  _.isEqual
);
// → [{ id: 2 }]
Try It Yourself

How It Works

  1. Deep match. Both arrays contain { id: 1, meta: { z: 1 } } → cancel.
  2. Exclusive. { id: 2 } has no partner → kept.
  3. References. New object literals still match under _.isEqual.
  4. Tip. Prefer xorBy if you only care about id, not full deep equality.

Example 5 — Identical Arrays Cancel

When every element finds a match, the result is empty.

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

How It Works

  1. Pairs. Each left object deep-equals a right object.
  2. Cancel all. Nothing exclusive remains.
  3. Result. Empty array [].
  4. Safe. Does not throw; inputs stay unchanged.

Browser & Runtime Support

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

_.xorWith()

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

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

Frequently Asked Questions

_.xorWith(...arrays, comparator) is like _.xor, but equality is decided by your comparator. Truthy means the two values match and cancel; exclusive values remain.
No. It returns a new array. All argument arrays stay unchanged.
Last. Pass every array first, then the comparator: _.xorWith(a, b, _.isEqual).
Return a truthy value when the two arguments should count as the same element for cancellation. Prefer symmetric predicates so order does not matter.
xorBy projects each value to one key. xorWith compares pairs with a binary predicate — better for deep equality or multi-field rules.
Use import xorWith from "lodash/xorWith" or require("lodash/xorWith") for tree-shaking. Full build: import _ from "lodash" then _.xorWith.

Next: _.zip()

Merge parallel arrays into tuples — the inverse of _.unzip.

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