将项ID添加到js if语句

时间:2013-08-20 08:53:35

标签: javascript jquery html css

我想在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.如果那种场景。

3 个答案:

答案 0 :(得分:2)

我认为条件engine.currentMap[toY][toX].item > 1在给定的情况下就足够了,好像有些东西比 1更大,这是最小的值那么你的条件是真的,你不需要检查其他条件。

答案 1 :(得分:0)

如果您只想要这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,它将始终在第一次检查时返回true(item > 1),因此不需要其余的。

答案 2 :(得分:0)

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

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