1

This is an array of objects I want to get a count of the duplicate objects in the array. What is the most concise and efficient way to find out duplicate objects?

[
{
  partNum: 'ACDC1007',
  brandName: 'Electric',
  supplierName: 'Electric',
},
{
  partNum: 'ACDC1007',
  brandName: 'Electric',
  supplierName: 'Electric',
},
{
  partNum: 'ACDC1007',
  brandName: 'Electric',
  supplierName: 'Electric',
},
{
  partNum: 'ACDC1009',
  brandName: 'Electric',
  supplierName: 'Electric',
},

{
  partNum: 'ACDC1000',
  brandName: 'Electric',
  supplierName: 'Electric',
}

]

I am looking for a way to modify the array of objects on the basis of partNum like this:

[
{
  partNum: 'ACDC1007',
  brandName: 'Electric',
  supplierName: 'Electric',
  count: 3
},
{
  partNum: 'ACDC1007',
  brandName: 'Electric',
  supplierName: 'Electric',
  count: 3
},
{
  partNum: 'ACDC1007',
  brandName: 'Electric',
  supplierName: 'Electric',
  count: 3
},
{
  partNum: 'ACDC1000',
  brandName: 'Electric',
  supplierName: 'Electric',
  count: 1
}
.............

] ```

Thanks
Nouman Shah
  • 103
  • 1
  • 9
  • Do you only want to add the property `count` in the all repeated objects. – DecPK Apr 27 '21 at 10:17
  • yes, I only want to add count property in each object. – Nouman Shah Apr 27 '21 at 10:19
  • 1
    Please add the code you've tried. There are many similar questions like: [Javascript - Counting duplicates in object array and storing the count as a new object](https://stackoverflow.com/q/45258566) and [Get duplicates in array of strings and count number of duplicates](https://stackoverflow.com/q/60172477) and [Count duplicates within an Array of Objects](https://stackoverflow.com/q/10541068) – adiga Apr 27 '21 at 10:19
  • What have you tried so far? – prettyInPink Apr 27 '21 at 10:20

1 Answers1

4

You can easily achieve this using reduce and map. First, count all the occurrences of all objects and then add the property dynamically.

const arr = [
  {
    partNum: "ACDC1007",
    brandName: "Electric",
    supplierName: "Electric",
  },
  {
    partNum: "ACDC1007",
    brandName: "Electric",
    supplierName: "Electric",
  },
  {
    partNum: "ACDC1007",
    brandName: "Electric",
    supplierName: "Electric",
  },
  {
    partNum: "ACDC1009",
    brandName: "Electric",
    supplierName: "Electric",
  },

  {
    partNum: "ACDC1000",
    brandName: "Electric",
    supplierName: "Electric",
  },
];

const countDict = arr.reduce((acc, curr) => {
  const { partNum } = curr;
  if (acc[partNum]) ++acc[partNum];
  else acc[partNum] = 1;
  return acc;
}, {});

const result = arr.map((obj) => {
  obj["count"] = countDict[obj.partNum];
  return obj;
});

console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • Depends how you cange. I mean what you gonna change in this array? Elaborate please – DecPK Apr 27 '21 at 10:40
  • can I achieve the same result. if I change the array to this. ? const arr = [ { produsts : { partNum: "ACDC1007", brandName: "Electric", supplierName: "Electric", }, }, { produsts : { partNum: "ACDC1007", brandName: "Electric", supplierName: "Electric", }, }, { produsts : { partNum: "ACDC1007", brandName: "Electric", supplierName: "Electric", }, } ] – Nouman Shah Apr 27 '21 at 10:44
  • It won't work for nested objects. It only work for no nested objects and as long as the object contain property `partNum` – DecPK Apr 27 '21 at 11:02
  • 1
    I have change the logic and it's working fine for me. const countDict = arr.reduce((acc, curr) => { const { partNum } = curr.products; if (acc[partNum]) ++acc[partNum]; else acc[partNum] = 1; return acc; }, {}); const result = arr.map((obj) => { obj.products["isMatched"] = countDict[obj.products.partNum]; return obj; }); – Nouman Shah Apr 27 '21 at 11:06