Filters yielded values by testing uniqueness with an index, key, or callback.
Optionally lets you set a numeric limit on total unique values yielded.
npm install unique-iterable-by --save
The module exports a single function.
Let’s say you have an iterable collection of person objects and you only want one person with any given name.
const uniqueIterableBy = require('unique-iterable-by')
const people = [
{name: 'John'},
{name: 'John'},
{name: 'Stephen'},
]
const u = uniqueIterableBy(people, 'name')
u.next().value // {name: 'John'}
u.next().value // {name: 'Stephen'}
u.next().done // true
Or you can use a callback that retrieves the significant value:
uniqueIterableBy(people, person => person.name)
You can also use the limit
argument to cap the number of total unique values yielded:
const u = uniqueIterableBy(people, 'name', {limit: 1})
u.next().value // {name: 'John'}
u.next().done // true