https://github.com/lamansky/unique-array-by
[Node.js] Filters an array by testing uniqueness with a callback, an index, or a key.
https://github.com/lamansky/unique-array-by
Last synced: 2 months ago
JSON representation
[Node.js] Filters an array by testing uniqueness with a callback, an index, or a key.
- Host: GitHub
- URL: https://github.com/lamansky/unique-array-by
- Owner: lamansky
- License: mit
- Created: 2017-12-20T14:44:38.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-20T15:26:43.000Z (over 8 years ago)
- Last Synced: 2025-03-16T02:06:34.153Z (over 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# unique-array-by
Filters an array by testing uniqueness with a callback, an index, or a key.
Optionally lets you set a numeric limit on total unique values returned.
If you merely need to remove duplicate values from an array, use the simpler [deduplicate](https://github.com/lamansky/deduplicate) module instead.
## Installation
```bash
npm install unique-array-by --save
```
The module exports a single function.
## Usage Example
Let’s say you have an array of person objects and you only want one person with any given name.
```javascript
const uniqueArrayBy = require('unique-array-by')
const people = [
{name: 'John'},
{name: 'John'},
{name: 'Stephen'},
]
uniqueArrayBy(people, 'name') // [{name: 'John'}, {name: 'Stephen'}]
```
Or you can use a callback that retrieves the significant value:
```javascript
uniqueArrayBy(people, person => person.name) // [{name: 'John'}, {name: 'Stephen'}]
```
You can also use the `limit` argument to cap the number of total unique values returned:
```javascript
uniqueArrayBy(people, 'name', {limit: 1}) // [{name: 'John'}]
```
## Related Projects
* [deduplicate](https://github.com/lamansky/deduplicate)
* [unique-iterable](https://github.com/lamansky/unique-iterable)
* [unique-iterable-by](https://github.com/lamansky/unique-iterable-by)
* [unique-map](https://github.com/lamansky/unique-map)
* [unique-map-by](https://github.com/lamansky/unique-map-by)