Use Set in filterNonUnique
This commit is contained in:
@ -5,11 +5,12 @@ tags: array,beginner
|
|||||||
|
|
||||||
Creates an array with the non-unique values filtered out.
|
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.
|
- Use `Array.prototype.filter()` to create an array containing only the unique values.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const filterNonUnique = arr =>
|
const filterNonUnique = arr =>
|
||||||
arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
[...new Set(arr)].filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
Reference in New Issue
Block a user