Javascript JS计数数组中的出现次数并按最高值排序

Javascript JS计数数组中的出现次数并按最高值排序,javascript,arrays,Javascript,Arrays,我正在尝试编写一个计票函数,我有一个对象数组,每个对象都有一个名为currentVote的属性,它有一个userId值。我想知道哪个userID拥有最多的投票权。使用lodash这是我目前得到的结果: function countVotes(players) { return new Promise(function(resolve, reject) { //votes should be empty, filled it with some sample values le

我正在尝试编写一个计票函数,我有一个对象数组,每个对象都有一个名为
currentVote
的属性,它有一个
userId
值。我想知道哪个userID拥有最多的投票权。使用lodash这是我目前得到的结果:

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);
  });
功能计数投票(玩家){
返回新承诺(功能(解决、拒绝){
//投票应该是空的,用一些样本值填充
让我们投票=['312139659792875521'、'3604453419863434'、'312139659792875521'、'3604453419863435'、'1'、'99999999999';
for(设i=0,j=players.length;i

如果我有多个投票数相同的
用户ID
,我如何选择一个随机值。目前似乎总是选择最小的ID?

你可以使用
.shuffle
.first
。可能类似于:

_.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()

您可以使用
.shuffle
.first
。可能类似于:

_.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()

基于您的问题,我相信您的目标如下,我使用数组方法sort对数组进行降序排序,并拾取第一个元素。希望这对您有所帮助

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);

基于您的问题,我相信您的目标如下,我使用数组方法sort对数组进行降序排序,并拾取第一个元素。希望这对您有所帮助

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);

与其选择第一个,不如检查有多少人拥有相同的票数,然后从中随机选择一个。为什么不需要使用承诺?使用.then()在其他地方。但是为什么?显示的代码是Synchronous,如果收到错误,承诺会修复它,而不是选择第一个,检查有多少人拥有相同的投票数,并从中随机选择一个。为什么不需要使用承诺?使用.then()在其他地方。但是为什么?显示的代码是同步的,我们得到了一个错误,承诺修复了它,检查这个,我编辑了代码,如果投票是相同的,我会得到随机值。检查这个,我编辑了代码,如果投票是相同的,我会得到随机值。