how to get every unique element in an array? in javascript

To get every unique element in an array, you can use the Set() constructor and the filter() method together.

index.tsx
let array = [1, 2, 3, 3, 4, 5, 5, 5, 6];

let uniqueArray = Array.from(new Set(array));

console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]
145 chars
6 lines

In the code above, we create a new Set object from our array, which only contains unique elements. Then, we convert this Set back into an array using the Array.from() method. This gives us an array of only the unique elements.

gistlibby LogSnag