Lodash _.uniqBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Creates a duplicate-free version of an array. Uniqueness is decided by the iteratee result for each element, compared with SameValueZero. Only the first element per projected key 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 _.uniqBy on an array. Start with this line.

Lodash
_.uniqBy(array, [iteratee=_.identity])
CallWhat it does
_.uniqBy(array, [iteratee=_.identity])Primary signature.
_.uniqBy(array)Call with an array argument.

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

Library declarations
Lodash
_.uniqBy(array, [iteratee=_.identity])

Parameters

ParameterDescription
arrayRequired. Source to dedupe
iterateeOptional. Default _.identity (like uniq)

Return Value

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

Lodash Examples

Example 1 — Official: Math.floor

Dedupe by integer part — from the Lodash docs.

Lodash
_.uniqBy([2.1, 1.2, 2.3], Math.floor);
// → [2.1, 1.2]
Try It Yourself

How It Works

  1. 2.1 → key 2. First time → keep the element 2.1.
  2. 1.2 → key 1. New key → keep 1.2.
  3. 2.3 → key 2. Key already seen → drop.
  4. Result. New array [2.1, 1.2]; source unchanged.

Example 2 — Property Shorthand

String iteratee uses _.property — keep first object per x.

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

How It Works

  1. First object. Key 1 → keep.
  2. Second. Key 2 → keep.
  3. Third. Key 1 again → drop (first wins).
  4. Shorthand. "x" is the same as o => o.x.

Example 3 — Function Iteratee

Use a callback when the key needs more than a property path.

Lodash
_.uniqBy(
  [
    { id: 1, label: "a" },
    { id: 2, label: "b" },
    { id: 1, label: "c" }
  ],
  function (row) { return row.id; }
);
// → [{ id: 1, label: "a" }, { id: 2, label: "b" }]
Try It Yourself

How It Works

  1. Keys. 1, then 2, then 1 again.
  2. First id 1. Label "a" stays.
  3. Later id 1. Label "c" is dropped.
  4. Tip. "id" shorthand would work the same here.

Example 4 — Unique by String Length

Any projection works — here, first string of each length.

Lodash
_.uniqBy(["a", "bb", "c", "dd"], "length");
// → ["a", "bb"]
Try It Yourself

How It Works

  1. "a" length 1 → keep.
  2. "bb" length 2 → keep.
  3. "c" length 1 → drop.
  4. "dd" length 2 → drop.

Example 5 — uniq vs uniqBy

Plain uniq compares references; uniqBy can compare a field.

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

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

_.uniqBy(rows, "x");
// → [{ x: 1 }]             // same projected key
Try It Yourself

How It Works

  1. uniq. Two object literals are different references → both kept.
  2. uniqBy. Both project to 1 → only the first kept.
  3. Choose By when uniqueness is about a field or formula.
  4. Choose With when you need a custom comparator (deep equal, etc.).

Browser & Runtime Support

_.uniqBy 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+

_.uniqBy()

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

Bottom line: Safe for production once Lodash 4.x is loaded. Prefer import uniqBy from "lodash/uniqBy" for smaller builds.

Frequently Asked Questions

_.uniqBy(array, iteratee) creates a duplicate-free array. Uniqueness is decided by the iteratee result for each element (SameValueZero). Only the first element per projected key is kept.
No. It returns a new array. The source is unchanged.
uniq compares whole values. uniqBy compares the iteratee output — e.g. Math.floor or a property like "id".
A function, a property string ("x"), a path, or identity when omitted. Lodash’s iteratee shorthand applies.
When the array is already sorted by the same iteratee so duplicates sit next to each other — sortedUniqBy only compares neighbors.
Use import uniqBy from "lodash/uniqBy" or require("lodash/uniqBy") for tree-shaking. Full build: import _ from "lodash" then _.uniqBy.

Next: _.uniqWith()

Dedupe with a custom comparator when a simple iteratee is not enough.

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