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]

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.