Skip to main content

uniq()

uniq<T>(array): T[]

Creates a duplicate-free version of an array, using SameValueZero for equality comparisons.

DEPRECATED

Use [...new Set(array)] or Array.from(new Set(array)) directly instead.


Type Parametersโ€‹

T: Tโ€‹

The type of elements in the array.


Parametersโ€‹

array: T[]โ€‹

The array to inspect.


Returns: T[]โ€‹

The new duplicate free array.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Also known asโ€‹

dedupe (Effect) ยท uniq (Lodash, es-toolkit, Remeda, Ramda, Antfu) ยท unique (Radashi, Modern Dash)


Exampleโ€‹

const numbers = [1, 2, 2, 3, 3, 3, 4];

// โŒ Deprecated approach
const uniqueNumbers = uniq(numbers);
console.log(uniqueNumbers); // [1, 2, 3, 4]

// โœ… Recommended approach
const uniqueNumbersNative = [...new Set(numbers)];
console.log(uniqueNumbersNative); // [1, 2, 3, 4]

// โœ… Alternative approach
const uniqueNumbersFrom = Array.from(new Set(numbers));
console.log(uniqueNumbersFrom); // [1, 2, 3, 4]

How it works?โ€‹

Creates a duplicate-free version of an array. Deprecated: Use [...new Set(array)] directly (ES2015).

Native Equivalentโ€‹

// โŒ uniq(arr)
// โœ… [...new Set(arr)]
// โœ… Array.from(new Set(arr))

Use Casesโ€‹

Remove duplicate values ๐Ÿ“Œโ€‹

Get unique values from an array.

const items = [1, 2, 2, 3, 3, 3, 4];
[...new Set(items)];
// => [1, 2, 3, 4]

Dedupe string listโ€‹

Remove duplicate strings from a list.

const tags = ["js", "ts", "js", "react", "ts"];
[...new Set(tags)];
// => ["js", "ts", "react"]

Get unique IDsโ€‹

Extract unique identifiers from objects.

const items = [{ id: 1 }, { id: 2 }, { id: 1 }];
[...new Set(items.map(i => i.id))];
// => [1, 2]