Lodash _.uniqBy() method
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
_.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.
Objects deduped by id
Later rows sharing an id disappear—the earliest object wins unchanged.
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" }] Numeric buckets with Math.floor
Decimals share buckets after flooring—the first representative per bucket survives.
import uniqBy from "lodash/uniqBy";
uniqBy([2.1, 1.2, 2.3], Math.floor);
// → [2.1, 1.2] Strings keyed by "length"
Property shorthand works on primitives wrapped by Lodash—here each string length defines uniqueness.
import uniqBy from "lodash/uniqBy";
uniqBy(["a", "bb", "c", "dd"], "length");
// → ["a", "bb"] 📋 _.uniqBy vs _.uniq, _.sortedUniqBy, _.unionBy
| API | Inputs | Use case |
|---|---|---|
_.uniqBy(array, iteratee) | Single array | Keyed dedupe with arbitrary ordering |
_.uniq(array) | Single array | Whole-element SameValueZero uniqueness |
_.sortedUniqBy(array, iteratee) | Sorted single array | Neighbor-only compares when projections cluster |
_.unionBy(...arrays, iteratee) | Multiple arrays | Merge sources before keyed dedupe |
_.uniqWith(array, comparator) | Single array | Pairwise predicates beyond iteratee keys |
Pitfalls to avoid
Freshness policy
First occurrence wins—reverse or sort upstream when newer duplicates must dominate.
Assuming sorted performance
uniqBy scans memory of prior keys; reach for sortedUniqBy after a compatible sort.
Pairwise equality
Iteratees emit scalar keys—not arbitrary pairwise compares—reshape inputs when possible, otherwise graduate to _.uniqWith().
❓ FAQ
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.
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.
6 people found this page helpful
