Lodash _.uniq() method
What you’ll learn
- How
_.uniq(array)strips duplicates without reordering survivors. - Why SameValueZero differs from naive
===scans around edge cases. - When sortedUniq, _.uniqBy(), or native
Setswaps make sense. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Knowing how _.union() merges multiple arrays clarifies that uniq operates on just one collection at a time.
- You understand shallow uniqueness—distinct object literals never dedupe unless values compare equal.
- You can open Try-it labs or run snippets locally.
Overview
_.uniq is the quickest Lodash helper when you only need a duplicate-free snapshot—telemetry batches, tag clouds, or playlist cleanup.
First wins
Earlier duplicates disappear silently while survivors keep their relative spacing.
Spec-aligned equality
SameValueZero keeps Lodash consistent with sibling helpers such as includes.
Immutable source
Inputs remain untouched for downstream consumers expecting the noisy original.
Syntax
_.uniq(array) - array: collection to inspect (array-like values coerce predictably).
- Returns: new array containing unique elements ordered by first appearance.
Collapse scattered repeats
Duplicates may be non-adjacent—uniq still removes later copies.
import uniq from "lodash/uniq";
uniq([2, 1, 2, 3, 1]);
// → [2, 1, 3] Ordering stays intuitive
Survivors surface in the same relative order they originally appeared—only duplicates vanish.
import uniq from "lodash/uniq";
uniq(["delta", "alpha", "bravo", "alpha", "charlie"]);
// → ["delta", "alpha", "bravo", "charlie"] Types stay distinct
1 and "1" remain separate slots because SameValueZero distinguishes types.
import uniq from "lodash/uniq";
uniq([1, "1", 1, "1", true]);
// → [1, "1", true] 📋 _.uniq vs _.sortedUniq, _.uniqBy, native Set
| API | Strength | Trade-off |
|---|---|---|
_.uniq(array) | Global dedupe with stable ordering | Tracks every prior value—costlier than neighbor-only scans |
_.sortedUniq(array) | Linear pass on sorted input | Requires sorted / contiguous duplicates |
_.uniqBy(array, iteratee) | Keyed uniqueness | Needs iteratee when duplicates differ visually |
[...new Set(array)] | Built-in primitive dedupe | No lodash array-like helpers or iteratee pipeline parity |
Pitfalls to avoid
Reference identity
Different object literals never dedupe—project ids via uniqBy instead.
Expecting sorted output
uniq does not sort; call sort separately when lexical order matters.
NaN display
Logging via JSON.stringify masks NaN—inspect live arrays in DevTools when debugging.
❓ FAQ
Summary
- Purpose:
_.uniq(array)returns a duplicate-free shallow copy using SameValueZero. - Siblings: consider sortedUniq for sorted buffers or uniqBy when rows key off fields.
- Next: Lodash _.uniqBy(), _.unionWith() (previous), or the array methods hub.
Need linear scans on already sorted data? Switch to _.sortedUniq()—it only compares neighbors, whereas plain uniq remembers every prior value. When duplicates share a stable field such as id, graduate to _.uniqBy().
6 people found this page helpful
