找出变量是否在数组中? [重复]

时间:2021-06-06 23:12:04

This question already has an answer here:

这个问题在这里已有答案:

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

TypeError:无效的'instanceof'操作数countryList

7 个解决方案

#1


16  

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 array and -1 if value is not present in array. so your code can be like this

它返回数组中值的索引,如果数组中不存在值,则返回-1。所以你的代码就像这样

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

#2


20  

You need to use Array.indexOf:

你需要使用Array.indexOf:

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

Please not that it is not supported in and before IE8 (and possibly other legacy browsers). Find out more about it here.

请注意,在IE8(以及可能的其他旧版浏览器)之前和之后都不支持它。在这里了解更多相关信息。

#3


4  

You seem to be looking for the Array.indexOf function.

您好像在寻找Array.indexOf函数。

#4


2  

instanceof is 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
  ...
}

#5


2  

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

#6


1  

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;
}

#7


0  

using jQuery

使用jQuery

You can use $.inArray()

你可以使用$ .inArray()

$.inArray(value, array)

return index of item or -1 if not found

返回项的索引,如果未找到,则返回-1

#1


16  

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 array and -1 if value is not present in array. so your code can be like this

它返回数组中值的索引,如果数组中不存在值,则返回-1。所以你的代码就像这样

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

#2


20  

You need to use Array.indexOf:

你需要使用Array.indexOf:

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

Please not that it is not supported in and before IE8 (and possibly other legacy browsers). Find out more about it here.

请注意,在IE8(以及可能的其他旧版浏览器)之前和之后都不支持它。在这里了解更多相关信息。

#3


4  

You seem to be looking for the Array.indexOf function.

您好像在寻找Array.indexOf函数。

#4


2  

instanceof is 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
  ...
}

#5


2  

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

#6


1  

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;
}

#7


0  

using jQuery

使用jQuery

You can use $.inArray()

你可以使用$ .inArray()

$.inArray(value, array)

return index of item or -1 if not found

返回项的索引,如果未找到,则返回-1