Lodash _.sortedUniqBy() method

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

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 sortBy pipelines 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

javascript
_.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.
1

Bucket floats with Math.floor

Decimals sorted ascending share floors in blocks—keep the first value per integer bucket.

javascript
import sortedUniqBy from "lodash/sortedUniqBy";

sortedUniqBy([1.1, 1.9, 2.0, 2.4], Math.floor);
// → [1.1, 2.0]
Try it Yourself
2

Objects keyed by id

Property shorthand picks the comparison field while retaining full objects from the first occurrence.

javascript
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" }]
Try it Yourself
3

Wrong sort order

If projections are not monotonic, duplicates separated by other keys survive—reach for sortBy plus sortedUniqBy or plain uniqBy.

javascript
import sortedUniqBy from "lodash/sortedUniqBy";

sortedUniqBy([{ id: 2 }, { id: 1 }, { id: 2 }], "id");
// → three rows — neighbors never repeat the same id
Try it Yourself

📋 _.sortedUniqBy vs _.uniqBy vs _.sortedUniq

APIEquality signalBest when
_.sortedUniqBy(array, iteratee)Adjacent projected valuesData already sorted by that projection
_.uniqBy(array, iteratee)Global memo of projectionsOrder arbitrary or unknown
_.sortedUniq(array)Adjacent raw elementsPrimitives sorted ascending without mapping

Pitfalls to avoid

Sort

Iteratee mismatch

Sorting by one field then deduping by another fragments duplicate bands unpredictably.

Stable

Survivor semantics

Only the first row per plateau remains—document that choice when merging conflicting payloads.

NaN

Projected NaN

SameValueZero still applies to iteratee outputs, so neighboring NaN projections collapse like numbers.

❓ FAQ

No. Lodash returns a new array containing one element per contiguous run of equal iteratee outputs.
Yes, whenever you need global uniqueness by that key. Only adjacent equal projections merge—gaps let duplicates slip through.
uniqBy remembers every iteratee result seen so far and works on arbitrary order. sortedUniqBy assumes ordering and only compares neighbors.
Functions, property names (including nested paths like "user.id"), or shorthand handled like other collection methods.
Use import sortedUniqBy from "lodash/sortedUniqBy" or require("lodash/sortedUniqBy") for tree-shaken bundles.

Summary

  • Purpose: _.sortedUniqBy(array, iteratee) removes consecutive duplicates after projecting each element.
  • Fallback: use uniqBy when ordering is not guaranteed or sort first with the same iteratee you pass here.
  • Next: Lodash _.tail(), _.sortedUniq() (previous), or the array methods hub.
Did you know?

Think of sortedUniqBy as sortedUniq on the projected keys—after _.sortedIndexBy()-consistent ordering, duplicate bands line up like plateaus on a chart.

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