I am using this code to get key from array with highest value
function getHighest(o){
var vals = [];
for(var i in o){
vals.push(o[i]);
}
var max = Math.max.apply(null, vals);
for(var i in o){
if(o[i] == max){
return i;
}
}
}
but sometimes there are more results in array with the same highest value e.g.
item1 = 4
item2 = 2
item3 = 1
item4 = 4
and code I am using returns only first result (item1). So my goal is to get key with highest value but in case there are more elements with the same highest value choose randomly one of them.