如何检查一个字符串数组在JavaScript中是否包含一个字符串?(复制)

时间:2021-03-02 01:33:30

This question already has an answer here:

这个问题已经有了答案:

I have a string array and one string. I'd like to test this string against the array values and apply a condition the result - if the array contains the string do "A", else do "B".

我有一个字符串数组和一个字符串。我想用数组值来测试这个字符串,并应用一个条件——如果数组包含字符串do“a”,那么else do“B”。

How can I do that?

我怎么做呢?

5 个解决方案

#1


300  

There is an indexOf method that all arrays have (except Internet Explorer 8 and below) that will return the index of an element in the array, or -1 if it's not in the array:

所有数组都有一个indexOf方法(除了Internet Explorer 8和以下),它将返回数组中元素的索引,或者如果不在数组中,则返回-1:

if (yourArray.indexOf("someString") > -1) {
    //In the array!
} else {
    //Not in the array
}

If you need to support old IE browsers, you can polyfill this method using the code in the MDN article.

如果您需要支持旧的IE浏览器,您可以使用MDN文章中的代码来填充这个方法。

#2


42  

You can use the indexOfmethod and "extend" the Array class with the method contains like this:

你可以使用indexOfmethod来“扩展”数组类,方法如下:

Array.prototype.contains = function(element){
    return this.indexOf(element) > -1;
};

with the following results:

用下面的结果:

["A", "B", "C"].contains("A") equals true

"A"、"B"、"C" .包含("A")等于true。

["A", "B", "C"].contains("D") equals false

包含("D") = false

#3


18  

var stringArray = ["String1", "String2", "String3"];

return (stringArray.indexOf(searchStr) > -1)

#4


9  

Create this function prototype:

创建这个函数原型:

Array.prototype.contains = function ( needle ) {
   for (i in this) {
      if (this[i] == needle) return true;
   }
   return false;
}

and then you can use following code to search in array x

然后可以使用以下代码在数组x中搜索

if (x.contains('searchedString')) {
    // do a
}
else
{
      // do b
}

#5


4  

This will do it for you:

这将为你做到:

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle)
            return true;
    }
    return false;
}

I found it in Stack Overflow question JavaScript equivalent of PHP's in_array().

在堆栈溢出问题中,我发现JavaScript等价于PHP的in_array()。

#1


300  

There is an indexOf method that all arrays have (except Internet Explorer 8 and below) that will return the index of an element in the array, or -1 if it's not in the array:

所有数组都有一个indexOf方法(除了Internet Explorer 8和以下),它将返回数组中元素的索引,或者如果不在数组中,则返回-1:

if (yourArray.indexOf("someString") > -1) {
    //In the array!
} else {
    //Not in the array
}

If you need to support old IE browsers, you can polyfill this method using the code in the MDN article.

如果您需要支持旧的IE浏览器,您可以使用MDN文章中的代码来填充这个方法。

#2


42  

You can use the indexOfmethod and "extend" the Array class with the method contains like this:

你可以使用indexOfmethod来“扩展”数组类,方法如下:

Array.prototype.contains = function(element){
    return this.indexOf(element) > -1;
};

with the following results:

用下面的结果:

["A", "B", "C"].contains("A") equals true

"A"、"B"、"C" .包含("A")等于true。

["A", "B", "C"].contains("D") equals false

包含("D") = false

#3


18  

var stringArray = ["String1", "String2", "String3"];

return (stringArray.indexOf(searchStr) > -1)

#4


9  

Create this function prototype:

创建这个函数原型:

Array.prototype.contains = function ( needle ) {
   for (i in this) {
      if (this[i] == needle) return true;
   }
   return false;
}

and then you can use following code to search in array x

然后可以使用以下代码在数组x中搜索

if (x.contains('searchedString')) {
    // do a
}
else
{
      // do b
}

#5


4  

This will do it for you:

这将为你做到:

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle)
            return true;
    }
    return false;
}

I found it in Stack Overflow question JavaScript equivalent of PHP's in_array().

在堆栈溢出问题中,我发现JavaScript等价于PHP的in_array()。