I have an array of objects like this:
var values = [
{
"price": 10,
"amount": 2
}, {
"price": 10,
"amount": 2
}, {
"price": 5,
"amount": 3
}, {
"price": 15,
"amount": 5
}
];
How can I sort it by the "price" value and when the "price" is the same I sum the "amount" value, so it will result like this:
var values = [
{
"price": 5,
"amount": 3
}, {
"price": 10,
"amount": 4
}, {
"price": 15,
"amount": 5
}
];
I can sort it by using:
values.sort((a, b) => a.price - b.price);
But I don't know how to sum the amount when the price is equal.
CodePudding user response:
Probably not most effective but most expressive:
[...new Set(values.map(({price}) => price))]
.sort((a, b) => a - b)
.map((p) => values
.filter(({price}) => p === price)
.reduce((t, e) => {
t.amount = e.amount;
return t;
}, {price: p, amount: 0}));
CodePudding user response:
You could take an object and get the values from it.
const
values = [{ price: 10, amount: 2 }, { price: 10, amount: 2 }, { price: 5, amount: 3 }, { price: 15, amount: 5 }],
grouped = Object.values(values.reduce((r, { price, amount }) => {
r[price] ??= { price, amount: 0 };
r[price].amount = amount;
return r;
}, {}));
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }