VOOZH about

URL: https://www.geeksforgeeks.org/javascript/how-to-get-duplicate-object-fields-from-collection-using-lodash/

⇱ How To Get Duplicate Object Fields From Collection Using Lodash? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How To Get Duplicate Object Fields From Collection Using Lodash?

Last Updated : 23 Jul, 2025

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'.


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.


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.


Output:

[
[
{ id: 1, name: 'Apple'},
{ id: 3, name: 'Apple'}
]
]
Comment