To find the maximum y value of the objects in array:
Math.max.apply(Math, array.map(function(o) { return o.y; }))
or in more modern JavaScript:
Math.max(...array.map(o => o.y))
Warning:
This method is not advisable, it is better to use reduce. With a large array, Math.max will be called with a large number of arguments, which can cause stack overflow.
To find the maximum y value of the objects in array:
Math.max.apply(Math, array.map(function(o) { return o.y; }))
or in more modern JavaScript:
Math.max(...array.map(o => o.y))
Warning:
This method is not advisable, it is better to use reduce. With a large array, Math.max will be called with a large number of arguments, which can cause stack overflow.
Find the object whose property "Y" has the greatest value in an array of objects
One way would be to use Array reduce..
const max = data.reduce(function(prev, current) {
return (prev && prev.y > current.y) ? prev : current
}) //returns object
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
- http://caniuse.com/#search=reduce (IE9 and above)
If you don't need to support IE (only Edge), or can use a pre-compiler such as Babel you could use the more terse syntax.
const max = data.reduce((prev, current) => (prev && prev.y > current.y) ? prev : current)
Note that if you have multiple objects sharing the same max value, it will only return the first matched object.