I have the following data
shots = [
{id: 1, amount: 2},
{id: 2, amount: 4}
]
Now I\'m trying to get the object which
There are couple of mistakes
shot.amount
instead of shot
Finally
shots.reduce((max, shot) =>
shot.amount > max ? shot.amount : max, 0);
Demo
var shots = [
{id: 1, amount: 2},
{id: 2, amount: 4}
];
var output = shots.reduce((max, shot) =>
shot.amount > max ? shot.amount : max, 0);
console.log( output );
Edit
If the entire object has to be returned, then initializer should be an object with amount
property
var shots = [
{id: 1, amount: 2},
{id: 2, amount: 4} ];
var output = shots.reduce((max, shot) =>
shot.amount > max.amount ? shot : max , {amount:0});
console.log( output );
There are two problems here, the first is that a reduce needs a return value. the second is you're comparing a number with an object.
Therefore, I think you need something like this:
// This will return the object with the highest amount.
let highest = shots.reduce((max, shot) => {
return shot.amount >= max.amount ? shot : max;
}, {
// The assumption here is that no amount is lower than a Double-precision float can go.
amount: Number.MIN_SAFE_INTEGER
});
// So, we can convert the object to the amount like so:
highest = highest.amount;
A clean short one liner would look something like so:
const highest = shots.sort((a, b) => b.amount - a.amount)[0]
Try this:
Updated
let highest = shots.reduce((max, shot) => {
return shot.amount > max.amount ? shot : max
}, {amount:0});
Cleaner 2 lines solution :)
const amounts = shots.map((a) => a.amount)
const highestAmount = Math.max(...amounts);
Update
Code above will return the highest amount. If you want to get the object that contains it, you will face the posibility that many objects contain the highest value. So you will need filter
.
const highestShots = shots.filter(shot => shot.amount === highestAmount)
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);
Sadly enough most answers here do not properly consider all cases
To properly handle negative values, you would have to seed with -Infinity
Secondly compare the largest value of that point with the new value
You'd get the following:
highest = shots.reduce((max, current) => current.amount >= max.amount ? current : max, {amount: -Infinity})
You can test this with
shots = [
{id: 1, amount: -2},
{id: 2, amount: -4},
{id: 3, amount: -4},
{id: 4, amount: -5},
]
highest = shots.reduce((max, current) => current.amount >= max.amount ? current : max, {amount: -Infinity}) // Returns id 1, amount -2
shots = [
{id: 1, amount: 2},
{id: 2, amount: 4},
{id: 3, amount: 4},
{id: 4, amount: 5},
]
highest = shots.reduce((max, current) => current.amount > max.amount ? current : max, {amount: -Infinity}) // Returns id 4 amount 5
Note that if the array is empty, you will get a value of {amount: -Infinity}
as a result, so you might want to handle the case where shots.length === 0
before the reduce