Lodash _.uniqBy() method

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

What you’ll learn

  • How _.uniqBy(array, iteratee) dedupes using projections instead of whole-element equality.
  • Why shorthand strings ("id"), paths, and functions all configure the same iteratee slot.
  • When _.uniq(), _.sortedUniqBy(), _.unionBy(), or _.uniqWith() is the sharper tool.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Skim _.uniq() first—uniqBy keeps the same first-wins policy but swaps plain SameValueZero checks for iteratee outputs.

  • You can picture uniqueness keys as JSON-stable fingerprints emitted per row.
  • You can open Try-it labs or run snippets locally.

Overview

_.uniqBy shines when duplicates disagree visually yet collide along an attribute—event envelopes keyed by traceId, UI rows keyed by sku, or rounded timestamps.

Keyed dedupe

Iteratee outputs—not reference identity—decide whether two slots collide.

First wins

Earlier survivors stick around while later duplicates drop silently.

Immutable source

Consumers downstream still see the noisy feed while you branch from the slim copy.

Syntax

javascript
_.uniqBy(array, iteratee)
  • array: collection to inspect.
  • iteratee: invoked per element to derive the uniqueness key (function, property name, path array, etc.).
  • Returns: new array with one element per distinct iteratee output, preserving first-seen order.
1

Objects deduped by id

Later rows sharing an id disappear—the earliest object wins unchanged.

javascript
import uniqBy from "lodash/uniqBy";

uniqBy(
  [
    { id: 1, label: "primary" },
    { id: 2, role: "backup" },
    { id: 1, label: "stale" }
  ],
  "id"
);
// → [{ id: 1, label: "primary" }, { id: 2, role: "backup" }]
Try it Yourself
2

Numeric buckets with Math.floor

Decimals share buckets after flooring—the first representative per bucket survives.

javascript
import uniqBy from "lodash/uniqBy";

uniqBy([2.1, 1.2, 2.3], Math.floor);
// → [2.1, 1.2]
Try it Yourself
3

Strings keyed by "length"

Property shorthand works on primitives wrapped by Lodash—here each string length defines uniqueness.

javascript
import uniqBy from "lodash/uniqBy";

uniqBy(["a", "bb", "c", "dd"], "length");
// → ["a", "bb"]
Try it Yourself

📋 _.uniqBy vs _.uniq, _.sortedUniqBy, _.unionBy

APIInputsUse case
_.uniqBy(array, iteratee)Single arrayKeyed dedupe with arbitrary ordering
_.uniq(array)Single arrayWhole-element SameValueZero uniqueness
_.sortedUniqBy(array, iteratee)Sorted single arrayNeighbor-only compares when projections cluster
_.unionBy(...arrays, iteratee)Multiple arraysMerge sources before keyed dedupe
_.uniqWith(array, comparator)Single arrayPairwise predicates beyond iteratee keys

Pitfalls to avoid

Stale

Freshness policy

First occurrence wins—reverse or sort upstream when newer duplicates must dominate.

Sort

Assuming sorted performance

uniqBy scans memory of prior keys; reach for sortedUniqBy after a compatible sort.

Cmp

Pairwise equality

Iteratees emit scalar keys—not arbitrary pairwise compares—reshape inputs when possible, otherwise graduate to _.uniqWith().

❓ FAQ

No. Lodash returns a new array; the source collection stays unchanged.
Always the second argument: _.uniqBy(array, iteratee). Unlike unionBy, there is only one collection slot.
The earlier element in array order wins; later duplicates are omitted.
Lodash uses SameValueZero on iteratee outputs—so NaN matches NaN and −0 matches +0.
Use import uniqBy from "lodash/uniqBy" or require("lodash/uniqBy") for tree-shaken bundles.

Summary

  • Purpose: _.uniqBy(array, iteratee) removes duplicates using iteratee outputs as uniqueness keys.
  • Ordering: survivors appear in first-seen order without sorting.
  • Next: Lodash _.uniqWith(), _.uniq() (previous), or the array methods hub.
Did you know?

When rows arrive sorted such that identical projections cluster together, _.sortedUniqBy() can replace the global memo that uniqBy maintains—just verify ordering aligns with the iteratee.

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