math.max object javascript code example
Example 1: javascript array find highest value of array of objects by key
Math.max.apply(Math, array.map(function(o) { return o.y; }))
Example 2: javascript get array min and max
function getArrayMax(array){
return Math.max.apply(null, array);
}
function getArrayMin(array){
return Math.min.apply(null, array);
}
var ages=[11, 54, 32, 92];
var maxAge=getArrayMax(ages);
var minAge=getArrayMin(ages);
Example 3: get object with max value javascript
let objects = [{id: 0, votes: 5}, {id: 1, votes: 3}, {id: 2, votes: 11}]
let maxObj = objects.reduce((max, obj) => (max.votes > obj.votes) ? max : obj);