Lodash _.sortedUniqBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.uniqBy, but designed and optimized for sorted arrays. It keeps the first element of each contiguous run that shares the same iteratee result. Part of Lodash Array since 4.0.0 (stable — not deprecated).

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.sortedUniqBy on an array. Start with this line.

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

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

Library declarations
Lodash
_.sortedUniqBy(array, [iteratee])

Parameters

ParameterDescription
arrayRequired. Sorted source to inspect
iterateeOptional. Invoked per element to produce the compare key

Return Value

Return ValueDescription
*New duplicate-free array

Lodash Examples

Example 1 — Official: Math.floor

Keep the first number in each integer floor group.

Lodash
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
// → [1.1, 2.3]
Try It Yourself

How It Works

  1. Sorted input. Values rise by floor group: ones, then twos.
  2. First of 1. 1.1 floors to 1 → kept.
  3. Same bucket. 1.2 also floors to 1 → skipped.
  4. New bucket. 2.3 floors to 2 → kept; 2.4 skipped.

Example 2 — Property Shorthand

On objects sorted by type, keep the first of each type.

Lodash
var items = [
  { type: "a", id: 1 },
  { type: "a", id: 2 },
  { type: "b", id: 3 },
  { type: "b", id: 4 }
];

_.sortedUniqBy(items, "type");
// → [{ type: "a", id: 1 }, { type: "b", id: 3 }]
Try It Yourself

How It Works

  1. Sort first. Items are ordered so equal type values sit together.
  2. String iteratee. "type" reads each object’s type field.
  3. Keep first. First "a" and first "b" are kept.
  4. Skip rest. Later objects with the same type are dropped.

Example 3 — Unsorted Pitfall

If equal keys are not adjacent, duplicates can survive.

Lodash
_.sortedUniqBy([1.1, 2.3, 1.2], Math.floor);
// → [1.1, 2.3, 1.2]  // 1.x not grouped — not fully unique

_.sortedUniqBy(_.sortBy([1.1, 2.3, 1.2], Math.floor), Math.floor);
// → [1.1, 2.3]
Try It Yourself

How It Works

  1. Gap. 1.1 and 1.2 are separated by 2.3.
  2. Neighbor check. Each step only compares to the previous kept key.
  3. Missed dup. 1.2 looks “new” after key 2 → kept incorrectly for global uniq.
  4. Fix. Sort by the iteratee first (or use _.uniqBy).

Example 4 — Already Unique Keys

When every adjacent key differs, the whole array is kept.

Lodash
_.sortedUniqBy([1.1, 2.2, 3.3], Math.floor);
// → [1.1, 2.2, 3.3]
Try It Yourself

How It Works

  1. Keys. Floors are 1, 2, 3 — all different.
  2. No runs. Nothing collapses.
  3. Result. A new array with the same three values.
  4. Still a copy. The source array is not mutated.

Example 5 — Empty Array

Deduping nothing yields an empty array.

Lodash
_.sortedUniqBy([], Math.floor);
// → []
Try It Yourself

How It Works

  1. Empty source. No elements to inspect.
  2. Result. Lodash returns [].
  3. Safe. No throw — fine in pipelines with optional data.
  4. Guard. Check length before assuming unique rows exist.

Browser & Runtime Support

_.sortedUniqBy is a Lodash 4.x Array helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.0.0+

_.sortedUniqBy()

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

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import sortedUniqBy from "lodash/sortedUniqBy" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

_.sortedUniqBy(array, iteratee) returns a new array with duplicates removed, comparing only neighbors by the iteratee result. It is optimized for arrays already sorted by that iteratee.
No. It returns a new array. Object elements inside are still shared by reference.
Yes, for correct global uniqueness. Only adjacent equal iteratee values collapse. If equal keys are not next to each other, duplicates can remain.
uniqBy tracks every iteratee result seen so far and works on any order. sortedUniqBy assumes sorted input and only compares neighbors — usually faster on large sorted lists.
Use import sortedUniqBy from "lodash/sortedUniqBy" or require("lodash/sortedUniqBy") so bundlers can tree-shake. Full build: import _ from "lodash" then _.sortedUniqBy.
Functions (like Math.floor), property name strings, or other Lodash iteratee shorthands. The iteratee is invoked per element.

Next: _.tail()

Get every element except the first — a small slice helper next in the track.

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

10 people found this page helpful