Lodash _.sortedUniqBy() method
What you’ll learn
- How
_.sortedUniqBy(array, iteratee)dedupes using projected neighbor comparisons. - Why sorting must align with the iteratee before calling this helper.
- When _.uniqBy() or
sortBypipelines replace hand-rolled loops. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.sortedUniq() first—the iteratee version applies the same neighbor-only rule to mapped values.
- You are comfortable with Lodash iteratee shorthand (
"prop", functions, paths). - You can open Try-it labs or run snippets locally.
Overview
_.sortedUniqBy shines after you sort rows by a stable key—version stamps, foreign keys, bucketed floats—then collapse redundant neighbors while preserving the first survivor per band.
Project & scan
Each element maps through the iteratee; consecutive identical projections collapse.
Deterministic picks
The leading row inside each duplicate band wins—handy when merging telemetry batches.
Non-destructive
Source arrays stay intact for auditing or alternate projections.
Syntax
_.sortedUniqBy(array, iteratee) - array: collection sorted so equal iteratee outputs appear in contiguous runs.
- iteratee: mapper invoked per element (function, property name, path, etc.).
- Returns: new array with one representative per consecutive equal projection.
Bucket floats with Math.floor
Decimals sorted ascending share floors in blocks—keep the first value per integer bucket.
import sortedUniqBy from "lodash/sortedUniqBy";
sortedUniqBy([1.1, 1.9, 2.0, 2.4], Math.floor);
// → [1.1, 2.0] Objects keyed by id
Property shorthand picks the comparison field while retaining full objects from the first occurrence.
import sortedUniqBy from "lodash/sortedUniqBy";
const rows = [
{ id: 1, label: "alpha" },
{ id: 1, label: "beta" },
{ id: 2, label: "gamma" }
];
sortedUniqBy(rows, "id");
// → [{ id: 1, label: "alpha" }, { id: 2, label: "gamma" }] Wrong sort order
If projections are not monotonic, duplicates separated by other keys survive—reach for sortBy plus sortedUniqBy or plain uniqBy.
import sortedUniqBy from "lodash/sortedUniqBy";
sortedUniqBy([{ id: 2 }, { id: 1 }, { id: 2 }], "id");
// → three rows — neighbors never repeat the same id 📋 _.sortedUniqBy vs _.uniqBy vs _.sortedUniq
| API | Equality signal | Best when |
|---|---|---|
_.sortedUniqBy(array, iteratee) | Adjacent projected values | Data already sorted by that projection |
_.uniqBy(array, iteratee) | Global memo of projections | Order arbitrary or unknown |
_.sortedUniq(array) | Adjacent raw elements | Primitives sorted ascending without mapping |
Pitfalls to avoid
Iteratee mismatch
Sorting by one field then deduping by another fragments duplicate bands unpredictably.
Survivor semantics
Only the first row per plateau remains—document that choice when merging conflicting payloads.
Projected NaN
SameValueZero still applies to iteratee outputs, so neighboring NaN projections collapse like numbers.
❓ FAQ
Summary
- Purpose:
_.sortedUniqBy(array, iteratee)removes consecutive duplicates after projecting each element. - Fallback: use
uniqBywhen ordering is not guaranteed or sort first with the same iteratee you pass here. - Next: Lodash _.tail(), _.sortedUniq() (previous), or the array methods hub.
Think of sortedUniqBy as sortedUniq on the projected keys—after _.sortedIndexBy()-consistent ordering, duplicate bands line up like plateaus on a chart.
6 people found this page helpful
