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]