正在添加项目id's到js-if语句

Adding item id's to the js if statement

本文关键字:js-if 语句 添加 项目 id      更新时间:2023-09-26

我想在follow-if语句-中添加更多的项Id

  if(engine.currentMap[toY] &&
  engine.currentMap[toY][toX] &&
  engine.currentMap[toY][toX].item > 1)
  {
  engine.keyboard.canInput = true;
  }

上面写着engine.currentMap[toY][toX].item>1)我还想添加数字Id,这样它就会像.item>1||45||78||45|| 23。如果这有点小题大做的话。

我认为条件engine.currentMap[toY][toX].item > 1在给定的场景中就足够了,因为如果某个值大于1,这将是最小值,那么您的条件为true,您不需要检查其他条件。

如果你只想要这5个项目,那么你可以使用这个:

if(engine.currentMap[toY] &&
    engine.currentMap[toY][toX] &&
    $.inArray(engine.currentMap[toY][toX].item, [0, 1, 45, 78, 23]) != -1) {
    engine.keyboard.canInput = true;
}

但如果您只想要.item > 1 || 45 || 78 || 45 || 23,那么在第一次检查(item > 1)时它将始终返回true,因此不需要其余部分。

要测试这些额外的id,请检查.item是否等于其中一个id,将它们添加到数组中,然后测试它们的存在:

&& ([45, 78, 45, 23].indexOf(engine.currentMap[toY][toX].item) === -1) {