javascript if语句,查看数组 [英] javascript If statement, looking through an array

查看:74
本文介绍了javascript if语句,查看数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天下午心灵已经空白,因为我的生活无法找到正确的方法:

Mind has gone blank this afternoon and cant for the life of me figure out the right way to do this:

if(i!="3" && i!="4" && i!="5" && i!="6" && i!="7" && i!="8" && i!="9" && i!="2" && i!="19" && i!="18" && i!="60" && i!="61" && i!="50" && i!="49" && i!="79" && i!="78" && i!="81" && i!="82" && i!="80" && i!="70" && i!="90" && i!="91" && i!="92" && i!="93" && i!="94"){

//do stuff

}

全部那些数字需要在数组中,然后我可以检查 i 是否不等于其中任何一个。

all those numbers need to be in an array, then i can check to see if "i" is not equal to any 1 of them.

谢谢>。<

推荐答案

var a = [3,4,5,6,7,8,9];

if ( a.indexOf( 2 ) == -1 ) { 
   // do stuff
}

indexOf 如果找不到该号码,则返回 -1 。如果找到,则返回 -1 以外的内容。如果你愿意,可以改变你的逻辑。

indexOf returns -1 if the number is not found. It returns something other than -1 if it is found. Change your logic if you want.

如果你需要字符串,请用引号括起数字( a = ['1','2'] )。我不知道你在处理什么,所以我给他们编号。

Wrap the numbers in quotes if you need strings ( a = ['1','2'] ). I don't know what you're dealing with so I made them numbers.

IE和其他不起眼的/旧浏览器需要 indexOf 方法:

IE and other obscure/older browsers will need the indexOf method:

if (!Array.prototype.indexOf)  
{  
  Array.prototype.indexOf = function(elt /*, from*/)  
  {  
    var len = this.length >>> 0;  

    var from = Number(arguments[1]) || 0;  
    from = (from < 0)  
         ? Math.ceil(from)  
         : Math.floor(from);  
    if (from < 0)  
      from += len;  

    for (; from < len; from++)  
    {  
      if (from in this &&  
          this[from] === elt)  
        return from;  
    }  
    return -1;  
  };  
}  

这篇关于javascript if语句,查看数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆