Below is the given array :
[
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client1",
"Product": "Pen",
"Quantity": "1120"
},
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client1",
"Product": "Pen",
"Quantity": "12003"
},
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client1",
"Product": "Ruler",
"Quantity": "34706"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client1",
"Product": "Ruler",
"Quantity": "6158"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client1",
"Product": "Pen",
"Quantity": "3702"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client1",
"Product": "Ruler",
"Quantity": "158"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client2",
"Product": "Pen",
"Quantity": "80"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client2",
"Product": "Ruler",
"Quantity": "17200"
},
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client3",
"Product": "Pen",
"Quantity": "393"
},
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client3",
"Product": "Pen",
"Quantity": "4073"
}
]
The goal is to combine objects with identical Date, Client, and Product.
This approach should be applicable for other scenarios such as merging object arrays based on matching Organisation && Client or just by Product.
The anticipated result will look like this :
[
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client1",
"Product": "Pen",
"Quantity": "13123"
},
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client1",
"Product": "Ruler",
"Quantity": "34706"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client1",
"Product": "Ruler",
"Quantity": "6316"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client1",
"Product": "Pen",
"Quantity": "3702"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client2",
"Product": "Pen",
"Quantity": "80"
},
{
"Date": "2019.07.07",
"Organisation": "A",
"Client": "Client2",
"Product": "Ruler",
"Quantity": "17200"
},
{
"Date": "2019.07.08",
"Organisation": "A",
"Client": "Client3",
"Product": "Pen",
"Quantity": "4466"
}
]
I attempted to implement a combination of reduce
and findIndex
, but it didn't yield the desired outcome of summing up similar values.
var data = obj.reduce((acc, v) => {
const index = acc.findIndex(o => {
return o["Date"] === v["Date"] &&
o["Product"] === v["Product"] &&
o["Client"] === v["Client"]
});
if (index >= 0) {
var originalQty = Number(acc[index]["Quantity"]);
originalQty += Number(v["Quantity"]);
acc[index]["Quantity"] = originalQty.toString();
}
else {
acc.push(v);
}
return acc;
}, []);
console.log(data);
return data;
UPDATE:
After some adjustments, I realized my code was correct; I just needed to modify the if statement to achieve the correct result.
if (index >= 0) {
var originalQty = Number(acc[index]["Quantity"]);
originalQty += Number(v["Quantity"]);
acc[index]["Quantity"] = originalQty.toString();
}
Perhaps there was an issue with how my processed object handled numbers stored as strings, which affected the calculations. It's strange that simple addition wasn't working when dealing with stringified numbers...
Anyhow, thank you all for your quick responses!
Each response provided valuable insights!