简体   繁体   English

nodejs javascript 查找条件

[英]nodejs javascript find with condition

Hello I have a queue of players and wanted to get the first player that met one of my two conditions, but I am unsure how to do that.你好,我有一队玩家,我想找到第一个满足我两个条件之一的玩家,但我不确定该怎么做。

First I get the value of a player (mmr) and wanted to get one (only one) player from the queue that had 5% more or less of this value首先,我得到一个玩家 (mmr) 的价值,并想从队列中获得一个(只有一个)玩家,该玩家的价值超过或低于这个价值的 5%

My condition I think is this to get 5% more or less:我认为我的条件是要获得或多或少的 5%:

  const condition = (5/100)*playerOne.mmr + playerOne.mmr
  const condition2 = (5/100)*playerOne.mmr - playerOne.mmr

my function:我的功能:

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

I have questions as I would get 5% more or less within my find我有疑问,因为我会在我的发现中得到或多或少 5%

Check out the documentation for the find function it details this use case.查看find函数的文档,它详细说明了这个用例。 Here's the line that you would want for the desired result.这是您想要获得所需结果的行。

const matchedPlayer = this.players.find((playerTwo) => playerTwo.mmr > condition || playerTwo.mmr > condition2);

You can try something like this:你可以尝试这样的事情:

if (player.condition1 > condition1*0.95 && player.condition1 < condition1*1.05) {
   // then you keep the player
} else {
   // else you remove it from the list
}

Also, I would highly reccomend you to give better identifiers to the variables.另外,我强烈建议您为变量提供更好的标识符。 Instead of condition1, something like age , height , weight .而不是 condition1,像ageheightweight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM