I'm creating a voting system and I have the following object:
var obj1 = { Mcds: 2, Pret: 2, kfc: 2, BK: 1 }
or (depending on the votes) it could be:
var obj2 = { Mcds: 2, Pret: 2, BK: 3 }
What I want is to display the most voted restaurant or restaurants.
I can achieve this with the obj2 example using the following code:
var obj2Keys = Object.keys(obj2);
var mostVoted = obj2Keys.reduce(function(previousValue, currentValue){
return obj2[previousValue] > obj2[currentValue] ? previousValue : currentValue;
}); // returns 'BK'
When I use the above code on the obj1 example I get 'kfc' what I want is 'kfc' 'Pret' 'Mcds' (in no particular order).