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.