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]