Unique values and filtering
This commit is contained in:
@ -1,19 +1,8 @@
|
||||
### Unique values of array
|
||||
|
||||
use ES6 `Set` and the `...rest` operator to discard all duplicated values.
|
||||
Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
|
||||
|
||||
```js
|
||||
const unique = c => [...new Set(c)]
|
||||
const unique = arr => [...new Set(arr)];
|
||||
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
|
||||
```
|
||||
|
||||
Use `Array.filter` for an array containing only the unique values
|
||||
|
||||
```js
|
||||
const unique = c => c.filter(i => c.indexOf(i) === c.lastIndexOf(i))
|
||||
// unique([1,2,2,3,4,4,5]) -> [1,3,5]
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user