Javascript 找出变量是否在数组中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13509669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find out if a variable is in an array?
提问by ngplayground
I have a variable:
我有一个变量:
var code = "de";
And I have an array:
我有一个数组:
var countryList = ["de","fr","it","es"];
Could someone help me as I need to check to see if the variable is inside the countryList array - my attempt is here:
有人可以帮助我,因为我需要检查变量是否在 countryList 数组内 - 我的尝试在这里:
if (code instanceof countryList) {
alert('value is Array!');
}
else {
alert('Not an array');
}
but I get the following error in console.log when it's run:
但是当它运行时,我在 console.log 中收到以下错误:
TypeError: invalid 'instanceof' operand countryList
类型错误:无效的“instanceof”操作数 countryList
回答by dsgriffin
回答by Sukhdeep Singh Handa
jQuery has a utility function to find whether an element exist in array or not
jQuery 有一个实用函数来查找元素是否存在于数组中
$.inArray(value, array)
It returns index of the value in arrayand -1if value is not present in array. so your code can be like this
如果数组中不存在值array,-1则返回值的索引。所以你的代码可以是这样的
if( $.inArray(code, countryList) != -1){
alert('value is Array!');
} else {
alert('Not an array');
}
回答by complex857
You seem to be looking for the Array.indexOffunction.
您似乎在寻找Array.indexOf函数。
回答by AlexStack
instanceofis for checking if an object is of a certain type (which is a whole different topic). So instead of the code you wrote, you should lookup in the array. You can either check every element like this:
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
...
}
Or you can use the simpler method of using indexOf()function. Every array has an indexOf()function that loops up an element and returnns its index in the array. If it can't find the element, it returns -1. So you check the output of indexOf()to see if it has found anything in the array that matches your string:
或者您可以使用更简单的使用indexOf()函数的方法。每个数组都有一个indexOf()函数来循环一个元素并返回它在数组中的索引。如果找不到该元素,则返回 -1。因此,您检查 的输出indexOf()以查看它是否在数组中找到与您的字符串匹配的任何内容:
if (countryList.indexOf(code) === -1) {
//the country code is not in the array
...
} else {
//the country code exists in the array
...
}
I would use the second algorithm because it is simpler. But the first algorithm is good too because it is more readable. Both have the same income, but the second one has better performance and is shorter. However, it is not supported in older browsers (IE<9).
我会使用第二种算法,因为它更简单。但是第一个算法也很好,因为它更具可读性。两者收入相同,但第二个性能更好,时间更短。但是,旧浏览器(IE<9)不支持它。
If you are using the JQuery library, you may use the inArray()function which works in all browsers. It is the same as indexOf()and retuns -1 if it doesn't find the element you are looking for. So you can use it like this:
如果您使用的是 JQuery 库,则可以使用inArray()适用于所有浏览器的功能。indexOf()如果没有找到您要查找的元素,则它与并返回 -1相同。所以你可以像这样使用它:
if ( $.inArray( code, countryList ) === -1) {
//the country code is not in the array
...
} else {
//the country code exists in the array
...
}
回答by Pranay Rana
In jquery you can make use of
在 jquery 中,您可以使用
jQuery.inArray()-Search for a specified value within an array and return its index (or -1 if not found).
jQuery.inArray() -在数组中搜索指定值并返回其索引(如果未找到,则返回 -1)。
if ($.inArray('de', countryList ) !== -1)
{
}
for javascript solution check existing How do I check if an array includes an object in JavaScript?
对于 javascript 解决方案检查现有 如何检查数组是否包含 JavaScript 中的对象?
Array.prototype.contains = function(k) {
for(p in this)
if(this[p] === k)
return true;
return false;
}
for example:
var list = ["one","two"];
list.contains("one") // returns true
回答by Bruno
For a pure JavaScript solution you could just traverse 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;
}
回答by Mahmoud Farahat
using jQuery
使用jQuery
You can use $.inArray()
您可以使用$.inArray()
$.inArray(value, array)
return index of item or -1 if not found
如果未找到,则返回项目的索引或 -1

