1.0.0 • Published 6 years ago

unique-array-by v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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

1.0.0

6 years ago