I have the following data
shots = [
{id: 1, amount: 2},
{id: 2, amount: 4}
]
Now I\'m trying to get the object which
You could check the amount property of both items and return the item with the greatest amount.
let highest = shots.reduce((a, b) => a.amount > b.amount ? a : b);
For same amount, you get the first object with the same amount only.
It does not work with an empty array.
If you like all objects with the same highest amount
, you could reduce the array with with two conditions.
let highest = shots.reduce((r, o) => {
if (!r || o.amount > r[0].amount) return [o];
if (o.amount === r[0].amount) r.push(o);
return r;
}, undefined);