Unique values and filtering

This commit is contained in:
Angelos Chalaris
2017-12-12 10:59:22 +02:00
parent 644be5756d
commit c793bf4062
3 changed files with 23 additions and 21 deletions

View File

@ -0,0 +1,8 @@
### Filter out non-unique values in an array
Use `Array.filter()` for an array containing only the unique values.
```js
const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
// unique([1,2,2,3,4,4,5]) -> [1,3,5]
```

View File

@ -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]
```