I have successfully removed the duplicates so far, but now I am stuck on how to sum the Total_Quantity. Is there a way to achieve this within the reduced method itself? Any help would be appreciated. Thank you.
const test = [
{
Item_Name: "Washing Machine",
Total_Quantity: 2,
},
{
Item_Name: "Washing Machine",
Total_Quantity: 2,
},
{
Item_Name: "Washing Machine",
Total_Quantity: 2,
},
{
Item_Name: "Washing Machine",
Total_Quantity: 2,
},
{
Item_Name: "Dryer Machine",
Total_Quantity: 5,
},
{
Item_Name: "Dryer Machine",
Total_Quantity: 5,
},
{
Item_Name: "Dryer Machine",
Total_Quantity: 5,
},
{
Item_Name: "Dryer Machine",
Total_Quantity: 5,
},
];
const dup = [
...test.reduce(
(map, obj) => map.set(obj.Item_Name, obj),
new Map()
).values(),
];
console.log(dup)
This is what the code should reflect:
const dup = [
{
Item_Name: "Washing Machine",
Total_Quantity: 8,
},
{
Item_Name: "Dryer Machine",
Total_Quantity: 20,
},
];