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
4

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

4 Comments

Depends how you cange. I mean what you gonna change in this array? Elaborate please
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", }, } ]
It won't work for nested objects. It only work for no nested objects and as long as the object contain property partNum
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; });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.