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]