NPM Smell 🍑💨

trivial and outdated NPM packages

array-unique trivial npm logo

Filters duplicate values from an array.

Weekly Downloads
13,258,323
Dependencies
0
Latest Release
8 years ago
The provided functionality does not warrant its own package
Trivial

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.