In the past, getting unique values meant pulling an external library or creating your own. Now, you can easily do that with Set and Array.from.

1
2
3
const books = ['Matilda', 'Charlie and the Chocolate Factory', 'Matilda', 'James and the Giant Peach', 'Matilda']
const uniqueBooks = Array.from(new Set(books))
// => ['Matilda', 'Charlie and the Chocolate Factory', 'James and the Giant Peach']

Set lets you store unique values and accepts any Object or primitive to be passed in to it’s initializer. When the data passed in has multiple items, it strips them out automatically.

Spread Operator

A simpler syntax exists if you use the spread operator:

1
2
const books = ['Matilda', 'Charlie and the Chocolate Factory', 'Matilda', 'James and the Giant Peach', 'Matilda']
const uniqueBooks = [...new Set(books)]