Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 找出变量是否在数组中?_Javascript_Arrays - Fatal编程技术网

Javascript 找出变量是否在数组中?

Javascript 找出变量是否在数组中?,javascript,arrays,Javascript,Arrays,我有一个变量: var code = "de"; 我有一个数组: var countryList = ["de","fr","it","es"]; 当我需要检查变量是否在countryList数组中时,是否有人可以帮助我-我的尝试如下: if (code instanceof countryList) { alert('value is Array!'); } else { alert('Not an array'); }

我有一个变量:

var code = "de";
我有一个数组:

var countryList = ["de","fr","it","es"];
当我需要检查变量是否在countryList数组中时,是否有人可以帮助我-我的尝试如下:

    if (code instanceof countryList) {
        alert('value is Array!');
    } 

    else {
        alert('Not an array');
    }
但在运行console.log时,我在其中遇到以下错误:

TypeError:无效的“instanceof”操作数国家/地区列表


您需要使用
数组。indexOf

if (countryList.indexOf(code) >= 0) {
   // do stuff here
}

请注意,IE8及其之前的版本(可能还有其他传统浏览器)不支持它。了解更多信息。

您似乎正在寻找该函数。

在jquery中,您可以使用

-在数组中搜索指定的值并返回其索引(如果未找到,则返回-1)

对于javascript解决方案,请检查现有


使用jQuery

你可以用


返回项或-1的索引(如果未找到)

jQuery有一个实用函数,用于查找数组中是否存在元素

$.inArray(value, array)
它返回
数组中值的索引
,如果数组中不存在值,则返回
-1
。所以你的代码可以是这样的

if( $.inArray(code, countryList) != -1){
     alert('value is Array!');
} else {
    alert('Not an array');
}

instanceof
用于检查对象是否属于某种类型(这是一个完全不同的主题)。因此,您应该在数组中查找,而不是编写代码。您可以这样检查每个元素:

var found = false;
for( var i = 0; i < countryList.length; i++ ) {
  if ( countryList[i] === code ) {
    found = true;
    break;
  }
}

if ( found ) {
  //the country code is not in the array
  ...
} else {
  //the country code exists in the array
  ...
}
if ( $.inArray( code, countryList ) === -1) {
  //the country code is not in the array
  ...
} else {
  //the country code exists in the array
  ...
}

我会使用第二种算法,因为它更简单。但是第一种算法也很好,因为它更具可读性。两者的收入相同,但第二个有更好的表现,而且更短。但是,在较旧的浏览器中不支持它(即,对于纯JavaScript解决方案,您只需遍历数组即可)

function contains( r, val ) {
    var i = 0, len = r.length;

    for(; i < len; i++ ) {
        if( r[i] === val ) {
            return i;
        }
     }
     return -1;
}
函数包含(r,val){
var i=0,len=r.长度;
对于(;i
请注意,
Array.indexOf
在IE8中不受支持-(可能还有其他传统浏览器).indexOf对于较旧的浏览器来说是非常简单的,如果这是一个要求的话。我认为这是jQuery,而不是核心Javascript。是的,这是jQuery,使用jQuery有什么问题!他没有提到他不想要jQuery。我认为
inArray()
是一种很好的技术,因为
indexOf()
在order浏览器中不可用。+1简单的问题y有-1?请对此发表评论我认为是谁投了反对票(不是我),之所以这样做是因为它最初只是用JavaScript标记的,并且无法假设他正在使用库。现在很明显,他想要JQuery,所以+1是正确的答案。@user1394965-问题是-1在OP-1 tag question和JQuery选择答案后发生,如果你想得到JQuery的解决方案…这是错误的,第二个相同的答案在这个答案y之前给出的,没有标记为asnwer…我想知道发生了什么,所以如果在接受答案之前给出的同一个asnwer没有被接受。。。
var found = false;
for( var i = 0; i < countryList.length; i++ ) {
  if ( countryList[i] === code ) {
    found = true;
    break;
  }
}

if ( found ) {
  //the country code is not in the array
  ...
} else {
  //the country code exists in the array
  ...
}
if (countryList.indexOf(code) === -1) {
  //the country code is not in the array
  ...
} else {
  //the country code exists in the array
  ...
}
if ( $.inArray( code, countryList ) === -1) {
  //the country code is not in the array
  ...
} else {
  //the country code exists in the array
  ...
}
function contains( r, val ) {
    var i = 0, len = r.length;

    for(; i < len; i++ ) {
        if( r[i] === val ) {
            return i;
        }
     }
     return -1;
}