Filters an array by testing uniqueness with a callback, an index, or a key.
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 module instead.
Installation
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.
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:
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:
uniqueArrayBy(people, 'name', {limit: 1}) // [{name: 'John'}]
Related Projects
HomePage
https://github.com/lamansky/unique-array-by