find-duplicates
v2.0.3
Published
Finds duplicate entries in a JavaScript array using an iteratee.
Downloads
117
Readme
find-duplicates
Finds duplicate entries in a JavaScript array using an iteratee.
API
type DuplicatePointerType<T> = {|
+index: number,
+value: T,
|};
const findDuplicates = <T: *>(members: $ReadOnlyArray<T>, iteratee: (T) => string) => $ReadOnlyArray<$ReadOnlyArray<DuplicatePointerType<T>>>;
Usage
findDuplicates
produces an array of duplicate input array entries as identified using iteratee function.
import findDuplicates from 'find-duplicates';
const haystack = [
{
id: 1,
name: 'a'
},
{
id: 2,
name: 'b'
},
{
id: 3,
name: 'a'
},
{
id: 4,
name: 'b'
},
{
id: 5,
name: 'c'
}
];
const duplicates = findDuplicates(haystack, (subject) => {
return subject.name;
});
duplicates;
[
[
{
index: 0,
value: {
id: 1,
name: 'a'
}
},
{
index: 2,
value: {
id: 3,
name: 'a'
}
}
],
[
{
index: 1,
value: {
id: 2,
name: 'b'
},
},
{
index: 3,
value: {
id: 4,
name: 'b'
},
},
]
]
Benchmark
Run benchmark before making changes and ensure that performance does not degrade after changes.
$ npm run benchmark