Reputation: 3
I have an empty array into which I am pushing values using javascript. I am able to find the max value of the array and set that to a variable using the following:
Array.max = function( array ){
return Math.max.apply( Math, array );
};
var maxX = Array.max(xArray);
How can I find the key associated with that value?
Upvotes: 0
Views: 10612
Reputation: 66405
Assuming that the values are unique, you could use Array.indexOf
:
var maxX = Array.max(xArray);
var index = xArray.indexOf(maxX);
If the keys are not unique, index
will contain the key of the first element found. If the value does not exist at all, the "key" will be -1
.
Upvotes: 4