I'm looking for a really quick, clean and efficient way to get the max "y" value in the following JSON slice:
[ { "x": "8/11/2009", "y": 0.026572007 }, { "x": "8/12/2009", "y": 0.025057454 }, { "x": "8/13/2009", "y": 0.024530916 }, { "x": "8/14/2009", "y": 0.031004457 } ]
Is a for-loop the only way to go about it? I'm keen on somehow using Math.max
.
Method 4: Using Collections.max() Define an empty ArrayList and add all elements of array to it. Pass this ArrayList to Collections. max(). The max() method of java.
Python Numpy – Get Maximum Value of Array Given a numpy array, you can find the maximum value of all the elements in the array. To get the maximum value of a Numpy Array, you can use numpy function numpy. max() function.
To find the maximum y
value of the objects in array
:
Math.max.apply(Math, array.map(function(o) { return o.y; }))
One way would be to use Array reduce..
const max = data.reduce(function(prev, current) { return (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.y > current.y) ? prev : current)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With