Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

javascript - 在一个条件下搜索数组中的值(Search for values ?in an array with one condition)

I have one array of players(我有一组球员)

and so I wanted to create a function to match each other following a condition(所以我想创建一个函数来匹配一个条件)

I started by thinking (get the first player on the queue) and trying to find another player that meets the condition(我开始思考(让第一个玩家进入队列),然后尝试寻找另一个符合条件的玩家)

But then I thought: If I don't match the first player, I will never move to the second match(但是后来我想:如果我不匹配第一个玩家,我将永远不会移到第二个比赛)

basically i need to get 2 players from a queue that meet a condition regardless of their position in the queue(基本上我需要从满足条件的队列中获得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;
  }

Something like this, I have two conditions |:(像这样,我有两个条件|:)

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

but in case I need to fetch two players with this condition without passing the first player(但是如果我需要在没有通过第一个玩家的情况下获取两名具有这种条件的玩家)

  ask by Felipe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is hard to tell why it is not working for you (are you sure there are even 2 players that match your parameters within your players list?)..(很难说出为什么它不适合您(您确定在您的播放器列表中甚至有2个与您的参数匹配的播放器?)。)

You can use the following example to help you, as it seems to be working for me.(您可以使用以下示例来帮助您,因为它似乎对我有用。)

 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> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...