https://github.com/sebastian-naicker/find-array-duplicates
utility to find duplicates in object arrays, easy
https://github.com/sebastian-naicker/find-array-duplicates
Last synced: 3 months ago
JSON representation
utility to find duplicates in object arrays, easy
- Host: GitHub
- URL: https://github.com/sebastian-naicker/find-array-duplicates
- Owner: sebastian-naicker
- License: mit
- Created: 2020-04-19T17:06:14.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T03:47:42.000Z (over 2 years ago)
- Last Synced: 2025-02-15T20:49:41.780Z (4 months ago)
- Language: HTML
- Size: 1.03 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# find-array-duplicates
A utility function to find all duplicates within a provided object array### Information
### Prerequisites
**Node ^12.x** [nodejs.org](https://nodejs.org)### Install
npm i find-array-duplicatesor
yarn add find-array-duplicates### Usage
`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 }
#### `=> single()`
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' }#### `=> all()`
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' }]#### `=> modify(callback)`
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#### `=> find(callback)`
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' }#### `=> map(callback)`
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 }]#### `=> filter(callback)`
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!