DEV Community

Dom Habersack
Dom Habersack

Posted on • Originally published at islovely.co

πŸ”₯ Filtering arrays to unique values

Arrays in JavaScript can contain the same value several times. Going through Set, we can filter them down to unique values.

const unique = array => [...new Set(array)]

// each value in this array is also how many of it are in there
unique([1, 2, 3, 3, 4, 4, 3, 4, 2, 4])  // β‡’ [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

For more tips like this, check out the full collection on my website: islovely.co/firetips

Top comments (0)