Underscore.js _.uniq() Method

Beginner
5 min read
Updated: Nov 2026
4 examples
Array

Definition

Produces a duplicate-free version of the array, using === to test equality. Only the first occurrence of each value is kept (official Underscore docs). Stable Array helper — not deprecated.

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

Syntax

Call _.uniq. Start with this line.

Underscore
_.uniq(array, [isSorted], [iteratee])
CallWhat it does
_.uniq(array, [isSorted], [iteratee])Primary signature.
_.uniq(...)Call with the arguments your case needs.

Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.

Library declarations
Underscore
_.uniq(array, [isSorted], [iteratee])

Parameters

ParameterDescription
arrayRequired. Source to de-duplicate
isSortedOptional. true enables the faster adjacent-compare path
iterateeOptional. Function (or shorthand) that computes the uniqueness key

Return Value

Return ValueDescription
*New array of unique values (first-seen)

Underscore.js Examples

Example 1 — Official Docs De-dupe

The classic Underscore example — keep the first of each value.

Underscore
_.uniq([1, 2, 1, 4, 1, 3]);
// → [1, 2, 4, 3]
Try It Yourself

How It Works

  1. First pass. Keep 1, then 2.
  2. Repeats. Later 1 values are skipped.
  3. New values. Keep 4, then 3.
  4. Order. Result follows first-seen order, not sorted order.

Example 2 — Sorted Fast Path

When the array is already sorted, pass true for a faster algorithm.

Underscore
_.uniq([1, 1, 2, 2, 2, 3, 4, 4], true);
// → [1, 2, 3, 4]
Try It Yourself

How It Works

  1. Sorted input. Duplicates sit next to each other.
  2. Fast path. Underscore only compares each value to the previous one.
  3. Result. Same unique list: [1, 2, 3, 4].
  4. Warning. Pass true only when the array really is sorted — otherwise results can be wrong.

Example 3 — Unique by Iteratee

Compute uniqueness from a transformed key; keep the original values.

Underscore
_.uniq([1, 2, 2.5, 3, 4, 7.5], false, Math.floor);
// → [1, 2, 3, 4, 7.5]
Try It Yourself

How It Works

  1. Keys. Math.floor turns each number into an integer key.
  2. Clash. 2 and 2.5 both floor to 2 — the later one is dropped.
  3. Kept value. The first original item for that key stays (2, not 2.5).
  4. Shortcut. You can also write _.uniq(arr, Math.floor) — a non-boolean second arg is treated as the iteratee.

Example 4 — One Record per Id

De-duplicate objects by a property using an iteratee.

Underscore
var users = [
  { id: 1, name: "Ava" },
  { id: 2, name: "Ben" },
  { id: 1, name: "Ava (dup)" }
];

_.uniq(users, false, function (u) { return u.id; });
// → [{ id: 1, name: "Ava" }, { id: 2, name: "Ben" }]
Try It Yourself

How It Works

  1. Key by id. The iteratee returns u.id for each user.
  2. First wins. The first user with id: 1 is kept; the duplicate is skipped.
  3. Objects by default. Without an iteratee, look-alike objects would both stay (different references).
  4. Use case. Clean API merges or lists where identity is a property, not the object itself.

Browser & Runtime Support

_.uniq (alias _.unique) ships with Underscore.js (1.x). It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore.

✓ Stable · Underscore 1.x

_.uniq()

Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.

100% With Underscore 1.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
_.uniq Underscore 1.x+

Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).

Frequently Asked Questions

_.uniq(array) returns a new array with duplicates removed, keeping only the first occurrence of each value in its original order.
Yes. _.unique is an alias for _.uniq — both behave the same.
With strict equality (===). Primitives match by value; objects match by reference.
If the array is already sorted, pass true as the second argument. Underscore then uses a faster path that only compares each value to the previous one.
An iteratee lets you decide uniqueness from a transformed key — for example Math.floor, or a property like user.id. The original item (not the key) is kept.
_.uniq de-duplicates one array. _.union merges several arrays, then de-duplicates. _.union(arr) behaves like _.uniq(arr).

Next: _.zip()

Merge arrays by index into paired tuples — great for coordinated data sources.

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