[英]nodejs javascript find with condition
你好,我有一隊玩家,我想找到第一個滿足我兩個條件之一的玩家,但我不確定該怎么做。
首先,我得到一個玩家 (mmr) 的價值,並想從隊列中獲得一個(只有一個)玩家,該玩家的價值超過或低於這個價值的 5%
我認為我的條件是要獲得或多或少的 5%:
const condition = (5/100)*playerOne.mmr + playerOne.mmr
const condition2 = (5/100)*playerOne.mmr - playerOne.mmr
我的功能:
makeMatch(playerOne) {
// make matched players
const condition = (5/100)*playerOne.mmr + playerOne.mmr
const condition2 = (5/100)*playerOne.mmr - playerOne.mmr
const player = playerOne;
const matchedPlayer = this.players.find();
// remove matched players from this.players
this.removePlayers(matchedPlayers);
// return new Match with matched players
return new Match(matchedPlayers);
}
我有疑問,因為我會在我的發現中得到或多或少 5%
查看find函數的文檔,它詳細說明了這個用例。 這是您想要獲得所需結果的行。
const matchedPlayer = this.players.find((playerTwo) => playerTwo.mmr > condition || playerTwo.mmr > condition2);
你可以嘗試這樣的事情:
if (player.condition1 > condition1*0.95 && player.condition1 < condition1*1.05) {
// then you keep the player
} else {
// else you remove it from the list
}
另外,我強烈建議您為變量提供更好的標識符。 而不是 condition1,像
age
,height
,weight
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.