'How to sort an array of objects by property values and sum when this value is equal?

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.



Solution 1:[1]

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; }

Solution 2:[2]

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}));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Florat