NPM Smell 🍑💨

trivial and outdated NPM packages

array-unique trivial npm logo

Filters duplicate values from an array.

Weekly Downloads
7,746,536
Dependencies
0
Latest Release
9 years ago
Better as a utility function
Trivial

About

This dependency filters duplicate values from an array.

This can be done natively 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.