I’m trying to build a questionnaire / personality test with jQuery.
I have an object in the end :
results = {
quality1 : 2,
quality2 : 3,
quality3 : 0,
quality4 : 3,
etc
}
I then want to retrieve the key with the higher value. No problem if there is only one matching result :
topScore = 0;
$.each(results, function(i,val) {
if ( results[i] > topScore ) {
topScore = results[i];
topQuality = i;
}
});
console.log('Higher quality : ' + topQuality)
but what if there are many ? (for instance, in the example, quality2 and quality4)
I’m kind of lost, here…
1 Answer