dimanche 8 mai 2016

JavaScript - output one of many top hits at random

So here I have a system that identifies the object with the highest count, however as we can see there are two objects that both have the highest count. What I want to be able to do is take the two top counts and output one at random. How can I do this?

 var objects = [
    {username: 'mark', count: 3},
    {username: 'dave', count: 5},
    {username: 'john', count: 5},
    {username: 'lucy', count: 2},
];

var res = objects.reduce(function(resObj, obj) {
  return resObj.count > obj.count ? resObj : obj
})

console.log(res);

Thanks!




Aucun commentaire:

Enregistrer un commentaire