Lodash _.uniqWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.uniq, but you pass a comparator invoked as (arrVal, othVal). Return truthy when two elements should count as duplicates. Only the first of each group is kept. Order follows the original array. Since Lodash 4.0.0.

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

Syntax

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

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

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

Library declarations
Lodash
_.uniqWith(array, [comparator])

Parameters

ParameterDescription
arrayRequired. Source to dedupe
comparatorOptional. (arrVal, othVal) => boolean

Return Value

Return ValueDescription
*The value returned by _.uniqWith().

Lodash Examples

Example 1 — Official: _.isEqual

Deep-equal objects collapse — from the Lodash docs.

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

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

How It Works

  1. First object. Nothing kept yet → keep.
  2. Second. Not deep-equal to the first → keep.
  3. Third. Deep-equal to the first → drop.
  4. Result. New array; source unchanged.

Example 2 — Field Comparator

Treat rows with the same sku as duplicates.

Lodash
_.uniqWith(
  [
    { sku: "A1", qty: 5 },
    { sku: "B2" },
    { sku: "A1", note: "dup" }
  ],
  function (a, b) { return a.sku === b.sku; }
);
// → [{ sku: "A1", qty: 5 }, { sku: "B2" }]
Try It Yourself

How It Works

  1. A1. First → keep (qty 5).
  2. B2. Different sku → keep.
  3. Second A1. Same sku → drop (first wins).
  4. Note. For a single field, _.uniqBy(…, "sku") is simpler.

Example 3 — Case-Insensitive Strings

Comparator ignores letter case.

Lodash
_.uniqWith(
  ["Apple", "banana", "APPLE", "Banana"],
  function (a, b) {
    return a.toLowerCase() === b.toLowerCase();
  }
);
// → ["Apple", "banana"]
Try It Yourself

How It Works

  1. Apple. Keep as first form.
  2. banana. Different lowercased key → keep.
  3. APPLE / Banana. Match earlier entries → drop.
  4. First-seen casing is what remains in the result.

Example 4 — Absolute Value

Treat numbers that share Math.abs as the same.

Lodash
_.uniqWith([1, -1, 2, -2, 1], function (a, b) {
  return Math.abs(a) === Math.abs(b);
});
// → [1, 2]
Try It Yourself

How It Works

  1. 1. Keep.
  2. -1. Same abs as 1 → drop.
  3. 2. New abs → keep; -2 and later 1 drop.
  4. Alt. _.uniqBy(arr, Math.abs) does the same for this case.

Example 5 — uniq vs uniqWith

Plain uniq keeps separate object references; isEqual collapses them.

Lodash
var rows = [{ x: 1 }, { x: 1 }];

_.uniq(rows);
// → [{ x: 1 }, { x: 1 }]

_.uniqWith(rows, _.isEqual);
// → [{ x: 1 }]
Try It Yourself

How It Works

  1. uniq. Two literals → two references → both kept.
  2. uniqWith + isEqual. Deep-equal → one kept.
  3. Choose With for deep or multi-rule equality.
  4. Choose By when a single projected key is enough.

Browser & Runtime Support

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

✓ Stable · Lodash 4.0.0+

_.uniqWith()

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

Bottom line: Safe for production once Lodash 4.x is loaded. Prefer import uniqWith from "lodash/uniqWith" for smaller builds. Pair with lodash/isEqual when you need deep equality.

Frequently Asked Questions

_.uniqWith(array, comparator) creates a duplicate-free array. The comparator (arrVal, othVal) returns truthy when two elements should count as the same. Only the first of each duplicate group is kept.
No. It returns a new array. The source is unchanged.
uniqBy projects each element to a key and compares keys. uniqWith compares pairs of elements with your predicate — useful for deep equality or multi-field rules.
Yes. _.uniqWith(objects, _.isEqual) is the common deep-equal pattern from the official docs.
The earliest element in array order wins; later matches are skipped.
Use import uniqWith from "lodash/uniqWith" or require("lodash/uniqWith") for tree-shaking. Full build: import _ from "lodash" then _.uniqWith.

Next: _.unzip()

Transpose an array of tuples into parallel columns (inverse of zip).

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

11 people found this page helpful