2

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.

2
  • Brute force would be easy Commented Feb 29, 2016 at 0:50
  • You don't store every number, you just loop through exactly once and keep a reference to whichever one is the current largest. Although you do need to think about what to do if more than one element has the same high number. Commented Feb 29, 2016 at 0:51

4 Answers 4

15

reduce will do the job:

var maxObj = arr.reduce(function(max, obj) {
  return obj.num > max.num? obj : max;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Reduce is what I was looking for.Thanks for this. Really elegant way to achieve the solution to my problem,
2

returns the object with the largest number:

var arr = [ { num: 0.5 }, { num: 1 }, { num: 0.35 }]
var res = Math.max.apply(Math,arr.map(function(o){return o.num;}))
var obj = arr.find(function(o){ return o.num == res; })
console.log(obj);

Comments

0

You could use lodash?

   var arr = [ { num: 0.5 }, { num: 1 }, { num: 0.35 }]
   var max = _.max(arr, function(item) {
      return item.num;
   });

per aquinas suggestion - switched _.sortBy with _.max (there is no _.maxBy anymore) plunkr: https://plnkr.co/edit/TYIou2GUUxRWcXoCXHnv?p=preview

2 Comments

Ack. Don't sort to find the largest number. If you are going to use lodash at all use maxby. _.maxBy(arr,"num")
@aquinas good point O(NlogN) is worse than O(N) - btw there is no _.maxBy anymore.. _.max is doing what is needed
0

Try using Math.max(), JSON.stringify() , String.prototype.match()

var arr = [ { num: 0.5 }, { num: 1 }, { num: 0.35 }];
var res = arr[(m = Math.max.apply(Math, n = JSON.stringify(arr).match(/(\d+\.\d+)|(\d+)/g))) && n.indexOf(m+"")];

12 Comments

But that doesn't return an object. OP wants to get the object with the Max value, not just the value.
@nnnnnn "But that doesn't return an object." ? Yes, it does; see bracket notation at res = arr[/* Math.max() */]
Maybe I'm just being particularly dense today, but aren't you trying to use the maximum .num property as an index into the original array? That won't work.
The "bit in brackets" returns the max value, that is then used as an index to the original array. It "works" because in the OP, the max value is 1 and the related object is at index 1. Make the max value say 3 at index 0 and see what happens.
Well, I guess you beat it into submission eventually. ;-) Not sure JSON.stringify is a sensible approach, and in /(\d+.\d+)|(\d+)/g the period should be quoted.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.