unique-by-property
v1.0.0
Published
Function supplied with an array, a property name and a callback returning an array of objects uniquely identified by that property's value.
Maintainers
Readme
A function filtering an array of objects by a single property, returning an array of objects uniquely identified by that property's value.
Require the module as follows:
const unique = require('unique-by-property')
Example
const arr = [ {id: 1, firstName: 'John', lastName: 'Mason'}, {id: 2, firstName: 'Matt', lastName: 'Milton'}, {id: 3, firstName: 'Andrew', lastName: 'Ivory'}, {id: 4, firstName: 'Ralph', lastName: 'Hughes'}, {id: 2, firstName: 'Maurizio', lastName: 'Bastoni'}, {id: 3, firstName: 'Dolph', lastName: 'Douglas'}, {id: 1, firstName: 'Buster', lastName: 'Hoost'}, {id: 1, firstName: 'Bradley', lastName: 'McDonald'}, {id: 4, firstName: 'Ryan', lastName: 'Camp'}, {id: 6, firstName: 'Reese', lastName: 'Fielding'}, {id: 2, firstName: 'Rose', lastName: 'Muffy'}, {id: 2, firstName: 'Martha', lastName: 'Cooper'}, {id: 1, firstName: 'Anna', lastName: 'Branson'} ]
unique (arr, 'id', (res) => {
console.log(res)
})
It will print: [ { id: 1, firstName: 'John', lastName: 'Mason' },
{ id: 2, firstName: 'Matt', lastName: 'Milton' },
{ id: 3, firstName: 'Andrew', lastName: 'Ivory' },
{ id: 4, firstName: 'Ralph', lastName: 'Hughes' },
{ id: 6, firstName: 'Reese', lastName: 'Fielding' } ]