I have next data:
const data = [
{amount: 100, percent: 1, days: 7},
{amount: 75, percent: 3, days: 8},
{amount: 75, percent: 3, days: 3},
{amount: 100, percent: 2, days: 5},
{amount: 100, percent: 1, days: 10},
{amount: 50, percent: 3, days: 9}
];
The task is to group objects by amount and percent, so that if amount and percent in different objects are the same I need to add their days. The result should look the next way:
const data = [
{amount: 100, percent: 1, days: 17},
{amount: 75, percent: 3, days: 11},
{amount: 100, percent: 2, days: 5},
{amount: 50, percent: 3, days: 9}
];
1st and 3rd objects are not grouped because of different percent. I have tried to use reduce method:
const result = data.reduce((groupedByAmount, current) => {
let amount = current.amount;
let percent= current.percent;
if (amount == amount && percent == percent) {
groupedByAmount.amount = amount;
current.days += current.days
}
return groupedByAmount
}, {
amount: 0,
percent: null,
days: 0
});
I don't how to check if amount and percent are the same, maybe map or filter would help