A utility function to find all duplicates within a provided object array
Node ^12.x nodejs.org
npm i find-array-duplicates
or
yarn add find-array-duplicates
duplicates
takes in 2 arguments arr
and property
. arr {Array}
should be an Array containing a list of objects of a similar structure.
property {String}
is the property within the object structure that you would like to check for duplicate values.
import duplicates from 'find-array-duplicates'
duplicates(arr, 'property')
// => { single, all, modify, map, filter, find }
Returns the first object of the filtered duplicates array
const names = [
{ 'age': 36, 'name': 'Bob' },
{ 'age': 40, 'name': 'Harry' },
{ 'age': 1, 'name': 'Bob' }
]
const results = duplicates(names, 'name').single()
// => { 'age': 36, 'name': 'Bob' }
Returns the entire list of duplicate objects on the property provided
const results = duplicates(names, 'name').all()
// => [{ 'age': 36, 'name': 'Bob' }, { 'age': 1, 'name': 'Bob' }]
Allows you to modify the output of the final result, the call back function is provided with the entire list of duplicate objects
Returns any
entirely in your control.
const results = duplicates(names, 'name').modify(dupes => dupes[0].age)
// => 36
Works exactly like Array.find
runs off the duplicate array
Returns an Object
based on the find
callback provided.
const results = duplicates(names, 'name').find(dupes => dupes.age === 1)
// => { 'age': 1, 'name': 'Bob' }
Works exactly like Array.map
runs off the duplicate array
Returns an Array
based on the map
callback provided.
const results = duplicates(names, 'name').map(({ name, age }, index) => { name, age, index })
// => [{ 'age': 1, 'name': 'Bob', index: 0 }, { 'age': 36, 'name': 'Bob', index: 1 }]
Works exactly like Array.filter
runs off the duplicate array.
Returns an Array
based on the filter
callback provided.
const results = duplicates(names, 'name').filter(dupes => dupes.age >= 1)
// => [{ 'age': 1, 'name': 'Bob' }, { 'age': 36, 'name': 'Bob' }]
Please report issues on the github issue page. Hope you enjoy!