Trax
Trax

Reputation: 1538

JS Count occurrences in array and sort by highest

I'm trying to write a vote counting function, I have an array of objects and each object has a property called currentVote which has a userId value. I want to find out which userID has the most votes. Using lodash this is what I got so far:

function countVotes(players) {
  return new Promise(function(resolve, reject) {
    //votes should be empty, filled it with some sample values
    let votes = ['312139659792875521','360445341989863434', '312139659792875521','360445341989863435','1','9999999999999999999999'];
    for (let i = 0, j = players.length; i < j; i++) {
      votes.push(players[i].currentVote);
    }
    let tally = _.chain(votes)
      .countBy()
      .toPairs()
      .sortBy(1).reverse()
      .map(0)
      .value()[0];
    resolve(tally);
  });

How can I pick a random value IF I have multiple userIds with the same number of votes. At the moment it seems that the smallest ID will always be picked?

Upvotes: 1

Views: 471

Answers (2)

Edison Trutwein
Edison Trutwein

Reputation: 775

Based on your question, I believe your object is as follow, I have used the array method sort to sort the array in descending and picked up the first element. Hope this helps.

let players = [{
                 "userId":1,
                 "currentVotes":2220
               },
               {
                 "userId":2,
                 "currentVotes":383830
               },
               {
                 "userId":3,
                 "currentVotes":6894740
               },
               {
                 "userId":4,
                 "currentVotes":6894740
               },
               {
                 "userId":5,
                 "currentVotes":1
               }
              ];


function getHighestVoteByRandowm(players){

    let arrSameVotes = [];

    let i = 0;
    do{
        arrSameVotes.push(players[i]);
        temp = players[i].currentVotes;
        i++;
    }while(players[i].currentVotes == temp);

    let rndTill = arrSameVotes.length - 1;
    let rndVal = Math.round(Math.random() * rndTill);

    return arrSameVotes[rndVal];

}

function sortVotes(a,b){
    return b.currentVotes - a.currentVotes;
}

let highestVotesPlayer = getHighestVoteByRandowm(players.sort(sortVotes));

console.log(highestVotesPlayer);

Upvotes: 3

ktilcu
ktilcu

Reputation: 3128

You could use _.shuffle and _.first. Maybe something like:

_.chain(votes).countBy()
  .groupBy() // added this to get a list of all the ones that have matching vote counts
  .toPairs()
  .sortBy(0)
  .reverse()
  .first() // most votes
  .last() // get the user ids
  .shuffle() // randomize order
  .first() // get whatever is first
  .value()

Upvotes: 1

Related Questions