Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 7x 7x 7x 7x 7x 31x 30x 7x 1x 2x 1x 1x 1x 1x 1x | /**
* This function returns an array of duplicate objects within a provided Object Array `arr`
* based on a given property `property`
*
* @since 1.0.0
* @category Array
* @param {Array} arr An Object Array
* @param {String} property A property in which to find duplicates off.
* @returns {Object} Returns an Object of utility functions to modify the return
* @example
*
* const names = [
* { 'age': 36, 'name': 'Bob' },
* { 'age': 40, 'name': 'Harry' },
* { 'age': 1, 'name': 'Bob' }
* ]
*
* duplicates(names, 'name').single()
* // => [{ 'age': 36, 'name': 'Bob' }] (Returns the first object of the filtered duplicates)
*/
function duplicates(arr, property) {
Iif (!Array.isArray(arr))
throw new Error(
`Please provide a valid Array, this could either be because you used an invalid array, or you provided a value which is not of type Array`
);
Iif (!property) throw new Error(`The property parameter cannot be empty`);
const clone = [...arr];
Iif (!clone[0].hasOwnProperty(property)) throw new Error(`Could not find property [${property}] in Object Array`);
const duplicates = [
...new Set(clone.map((val) => val[property]).filter((item, index, iteratee) => iteratee.indexOf(item) !== index))
];
const flattenedDupes = duplicates.map((val) => clone.filter((item) => item[property] === val)).flat();
return {
single: () => flattenedDupes[0],
all: () => flattenedDupes,
modify: (cb) => cb(flattenedDupes),
map: (cb) => flattenedDupes.map(cb),
filter: (cb) => flattenedDupes.filter(cb),
find: (cb) => flattenedDupes.find(cb)
};
}
module.exports = duplicates
|