Lodash _.uniqWith() method

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

What you’ll learn

  • How _.uniqWith(array, comparator) removes duplicates using a binary predicate instead of projections alone.
  • Why comparators follow the same (arrVal, othVal) contract as unionWith and differenceWith.
  • When _.uniqBy() or plain _.uniq() stays simpler than bespoke compares.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

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

  • You are comfortable writing pure functions that compare two values at a time.
  • You can open Try-it labs or run snippets locally.

Overview

_.uniqWith closes the gap when duplicates need contextual logic—deep structural equality, fuzzy numeric bands, or case-insensitive tokens inside one noisy array.

Binary compares

You decide when two elements collide—no forced single-field projection.

First wins

Earlier survivors remain as-is while later duplicates disappear.

Immutable source

The original array stays intact for auditing or retries.

Syntax

javascript
_.uniqWith(array, comparator)
  • array: collection to dedupe.
  • comparator: invoked as (arrVal, othVal); truthy means treat the pair as duplicates.
  • Returns: new array containing first occurrences under your comparator.
1

Rows merged by shared sku

Different object shapes still collide when the comparator narrows on a stable identifier inside one list.

javascript
import uniqWith from "lodash/uniqWith";

uniqWith(
  [
    { sku: "A1", qty: 5 },
    { sku: "B2" },
    { sku: "A1", note: "duplicate" }
  ],
  (a, b) => a.sku === b.sku
);
// → [{ sku: "A1", qty: 5 }, { sku: "B2" }]
Try it Yourself
2

Deep structure with _.isEqual

Distinct references with identical nested payloads collapse when Lodash deems them deeply equivalent.

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

uniqWith(
  [{ id: 7, meta: { tier: "gold" } }, { id: 9 }, { id: 7, meta: { tier: "gold" } }],
  isEqual
);
// → [{ id: 7, meta: { tier: "gold" } }, { id: 9 }]
Try it Yourself
3

Case-insensitive strings

Primitives compare fine—fold casing once inside the comparator without wrapping strings in objects.

javascript
import uniqWith from "lodash/uniqWith";

uniqWith(["a", "B", "A", "b"], (x, y) => x.toLowerCase() === y.toLowerCase());
// → ["a", "B"]
Try it Yourself

📋 _.uniqWith vs uniq, uniqBy, unionWith

APIEquality signalBest when
_.uniqWith(array, comparator)Binary predicateCustom pairwise rules on one array
_.uniq(array)SameValueZeroPrimitive/reference-stable uniqueness
_.uniqBy(array, iteratee)Projected scalar keysSingle-field fingerprints suffice
_.unionWith(...arrays, comparator)Binary predicateMultiple arrays merged before dedupe

Pitfalls to avoid

Perf

Comparator cost

Rich predicates such as deep equality can dominate runtime—measure large batches before production.

Logic

Asymmetric compares

Weird predicates that are not symmetric produce confusing survivors—keep rules predictable.

Stale

Freshness policy

First occurrence wins—reverse or sort upstream when newer twins should replace older ones.

❓ FAQ

No. Lodash returns a new array; your source stays unchanged.
Return a truthy value when the two arguments should be treated as duplicates. Symmetric predicates are easiest to reason about.
Always last: _.uniqWith(array, comparator)—there is only one collection argument.
The earliest element in array order wins; later matches are skipped.
Use import uniqWith from "lodash/uniqWith" or require("lodash/uniqWith") for tree-shaken bundles.

Summary

  • Purpose: _.uniqWith(array, comparator) dedupes using your binary predicate while preserving first-seen order.
  • Contrast: prefer uniqBy when a lone iteratee expresses uniqueness cleanly.
  • Next: Lodash _.unzip(), _.uniqBy() (previous), or the array methods hub.
Did you know?

The comparator matches _.unionWith() and _.differenceWith(): return truthy when two values should count as the same logical row—even when references or projections disagree.

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