In need the Object with maximum a+b value from myArray
var myArray = [{a:5,b:10},{a:10,b:7},{a:8,b:5}];
Right now I have something that returns me the index:
var max = [], maxIndex;
myArray.map(x=>max.push(x.a + x.b))
maxIndex = max.indexOf( Math.max.apply(Math, max))
I need something that returns the Object and not its index, so far working with
var maxObject = myArray.map(x=>x.a + x.b).reduce((x,y)=>x>y)
returning false.
myArray[maxIndex]Array.reduceyet.myArray.reduce((x,y) => x.a + x.b > y.a + y.b ? x : y)– you need to returnxoryand not the boolean from the comparison, and if you reduce the array of sums you get the highest sum and not the object.