在具有一个条件的数组中搜索值

时间:2019-11-29 22:56:13

标签: javascript node.js

我有一组球员

所以我想创建一个函数来根据条件相互匹配

我从思考(让第一个玩家进入队列)开始,然后尝试寻找另一个符合条件的玩家

但是后来我想:如果我不匹配第一个玩家,我将永远不会移到第二个比赛

基本上,我需要从满足条件的队列中获得2名球员,无论他们在队列中的位置如何

  searching(id) {
    const firstPLayer = this.players.find(p => p.id == id)
    const { mmr } = firstPLayer
    const secondPlayer = this.players.find((playerTwo) => playerTwo.mmr < (5 / 100) * mmr + mmr && playerTwo.mmr > mmr - ((5 / 100) * mmr) && playerTwo.id != firstPLayer.id);
    if(!secondPlayer){
      return null;
    }
    const matchedPlayers = [
      firstPLayer,
      secondPlayer
    ]
    // remove matched players from this.players
    this.removePlayers(matchedPlayers);
    // return new Match with matched players
    return matchedPlayers;
  }

类似这样的情况,我有两个条件|:

  

playerTwo.mmr <(5/100)* mmr + mmr && playerTwo.mmr> mmr-((5/100)*   mmr)

但是如果我需要在不通过第一个玩家的情况下以这种条件获取两名玩家

1 个答案:

答案 0 :(得分:1)

很难说出为什么它对您不起作用(您确定在您的播放器列表中甚至有2个与您的参数匹配的播放器吗?)。

您可以使用以下示例来帮助您,因为它似乎对我有用。

class Queue {
  constructor() {
    this.players = fetchPlayers();
  }
  
  removePlayers(playersToRemove) {
    playersToRemove.forEach(player => {
      const index = this.players.indexOf(player);
      this.players.splice(index, 1);
    })
  }
  
  getRandomPlayer() {
    return this.players[Math.floor(Math.random() * this.players.length)];
  }
  
  calculateLessThanPercentage(player) {
    return 5 / 100 * player.mmr + player.mmr 
  }
    
  calculateGreaterThanPercentage(player) {
    return player.mmr - 5 / 100 * player.mmr
  }

  searching() {
    const firstPlayer = this.getRandomPlayer();
    
    const secondPlayer = this.players.find(
      playerTwo =>
        playerTwo.mmr < this.calculateLessThanPercentage(firstPlayer) &&
        playerTwo.mmr > this.calculateGreaterThanPercentage(firstPlayer) &&
        playerTwo.id != firstPlayer.id
    );
    
    if (!secondPlayer) {
      return null;
    }
    
    const matchedPlayers = [firstPlayer, secondPlayer];
    this.removePlayers(matchedPlayers);
    return matchedPlayers;
  }
}

const ActiveQueue = new Queue();

const getPlayersButton = document.getElementById("getPlayers");
const playersResults = document.getElementById("players");
const allPlayers = document.getElementById("allPlayers");

getPlayersButton.addEventListener("click", () => {
  getPlayers(ActiveQueue, playersResults, allPlayers)
});

// getPlayersButton click event handler
function getPlayers(queue, resultsElement, allPlayersElement) {
  const searchResults = queue.searching();
  resultsElement.innerHTML = JSON.stringify(searchResults, null, 2);
  allPlayersElement.innerHTML = "Remaining Players: " + JSON.stringify(queue.players, null, 2);
}

// to get data
function fetchPlayers() {
  return [
    {
      name: "Player One",
      id: 1,
      mmr: 500
    },
    {
      name: "Player Two",
      id: 2,
      mmr: 800
    },
    {
      name: "Player Three",
      id: 3,
      mmr: 810
    },
    {
      name: "Player Four",
      id: 4,
      mmr: 815
    },
    {
      name: "Player Five",
      id: 5,
      mmr: 510
    },
    {
      name: "Player Six",
      id: 6,
      mmr: 540
    },
    {
      name: "Player Seven",
      id: 7,
      mmr: 530
    },
    {
      name: "Player Eight",
      id: 8,
      mmr: 520
    }
  ];
}
button {
  margin-top: 10px;
}
Click to get players (scroll all the way down to see remaining players)
<br />
<button id="getPlayers">Get Players</button>
<pre id="players"></pre>
<br />
<hr />
<pre id="allPlayers"></pre>