How To Get Duplicate Object Fields From Collection Using Lodash?
Last Updated :
03 Oct, 2024
Finding duplicate object fields in a collection is a common problem when working with large datasets. Lodash provides several utilities that can help identify these duplicates easily by comparing properties within arrays of objects. In this article, we’ll explore different approaches to g duplicate object fields from a collection using Lodash.
Below are the following approaches to get duplicate object fields from collection using Lodash:
1. Using _.groupBy() and _.filter()
The _.groupBy() method creates an object composed of keys generated from the results of running each element of a collection through an iterate. Then, we use _.filter() to find groups with more than one object, indicating duplicates.
Syntax:
_.groupBy(collection, iteratee);
_.filter(collection, predicate);
Example: Here, we group the collection by the name field and then filter groups that have more than one object. The result is an array containing the duplicate objects with the name 'Apple'.
JavaScript
const _ = require('lodash');
// Sample collection of objects
const collection = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Mango' },
{ id: 3, name: 'Apple' },
{ id: 4, name: 'Strawberry' }
];
// Group objects by 'name'
const grouped = _.groupBy(collection, 'name');
// Filter out groups with more than one item (fruits)
const fruits = _.filter(grouped, group => group.length > 1);
console.log(fruits);
Output:
[
[
{ id: 1, name: 'Apple'},
{ id: 3, name: 'Apple'}
]
]
2. Using _.countBy() and _.filter()
The _.countBy() method creates an object where keys are generated from the results of running each element through the iterate, and the values are the number of times the key appears. Then, we use _.filter() to extract the fields that have a count greater than 1.
Syntax:
_.countBy(collection, iteratee);
_.filter(collection, predicate);
Example: Here we use the _.countBy() method to create an object showing how many times each name occurs in the collection. The result is than filtered to return only the names that appear more than once. In this case, the name 'Apple' is duplicated.
JavaScript
const _ = require('lodash');
// Sample collection of objects
const collection = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Apple' },
{ id: 4, name: 'Strawberry' }
];
// Count occurrences of names
const nameCounts = _.countBy(collection, 'name');
// Filter the names that have more than one occurrence
const dupNames = _.filter(_.keys(nameCounts), name => nameCounts[name] > 1);
console.log(dupNames);
Output:
[ 'Apple' ]
3. Using _.reduce()
The _.reduce() method is a more flexible way to iterate over the collection and manually check for duplicate fields by building a map of occurrences.
Syntax:
_.reduce(collection, iteratee, accumulator);
Example: Here, we use _.reduce() to manually count occurrences of each name in the collection. We then filter the result to return only the names with more than one occurrence, resulting in the duplicate objects for 'Apple.
JavaScript
const _ = require('lodash');
// Sample collection of objects
const collection = [
{ id: 1, name: 'Apple' },
{ id: 2, name: 'Banana' },
{ id: 3, name: 'Apple' },
{ id: 4, name: 'Strawberry' }
];
// Reduce the collection to count name occurrences
const nameOcc = _.reduce(collection, (result, item) => {
if (!result[item.name]) {
result[item.name] = [];
}
result[item.name].push(item);
return result;
}, {});
// Filter the reduced result to get duplicates
const dup = _.filter(nameOcc, items => items.length > 1);
console.log(dup);
Output:
[
[
{ id: 1, name: 'Apple'},
{ id: 3, name: 'Apple'}
]
]