Answers for "how to find max integer value with key from an object in javascript"

13

find max of array of objects key

Math.max.apply(Math, array.map(function(o) { return o.y; }))
Posted by: Guest on March-05-2020
-1

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);

/* `max` is always the object with the highest value so far. 
 * If `obj` has a higher value than `max`, then it becomes `max` on the next iteration.
 * So here:
 *  |  max = {id: 0, votes: 5},   obj = {id: 1, votes: 3}
 *  |  max = {id: 0, votes: 5},   obj = {id: 2, votes: 11}
 * reduced = {id: 2, votes: 11}
 */
Posted by: Guest on January-20-2021

Code answers related to "how to find max integer value with key from an object in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language