Use Set in filterNonUnique

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-11-02 19:40:45 +02:00
parent 652cf52889
commit da62efc18b

View File

@ -5,11 +5,12 @@ tags: array,beginner
Creates an array with the non-unique values filtered out.
- Use `new Set()` and the spread operator (`...`) to create an array of the unique values in `arr`.
- Use `Array.prototype.filter()` to create an array containing only the unique values.
```js
const filterNonUnique = arr =>
arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
[...new Set(arr)].filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
```
```js