About
This dependency filters duplicate values from an array.
This can be done concisely and natively by using the Set
object and the `spread operator“:
const arr1 = [1, 2, 3, 4, 5, 5, 5];
const unique1 = [...new Set(arr)]; // [1, 2, 3, 4, 5]
const arr2 = ["a", "b", "c", "c", "d", "d", "d"];
const unique1 = [...new Set(arr)]; // ["a", "b", "c", "d"]
As function:
function unique(arr) {
return [...new Set(arr)];
}
Without the spread operator
:
function unique(arr) {
return Array.from(new Set(arr));
}
Without the spread operator
and Set
object:
function unique(arr) {
return arr.filter((value, index, self) => self.indexOf(value) === index);
}
The Set
object and spread operator
are supported by all modern browsers and Node.js versions since about 7 years.