What's the easiest way to find the largest number in an array of objects and return that object?
var arr = [ { num: 0.5 }, { num: 1 }, { num: 0.35 }]
Tried to use forEach but couldn't work out a way to do this, other than storing every number and comparing them.
Any help is appreciated. Thanks in advance.
reduce will do the job:
var maxObj = arr.reduce(function(max, obj) {
return obj.num > max.num? obj : max;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With