如何在 JavaScript/jQuery 中查找数组是否包含特定字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6116474/
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
How to find if an array contains a specific string in JavaScript/jQuery?
提问by Cofey
Can someone tell me how to detect if "specialword"
appears in an array? Example:
有人能告诉我如何检测是否"specialword"
出现在数组中吗?例子:
categories: [
"specialword"
"word1"
"word2"
]
回答by James
You really don't need jQuery for this.
你真的不需要jQuery。
var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles") > -1);
Hint: indexOf returns a number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs
提示:indexOf 返回一个数字,表示指定搜索值第一次出现的位置,如果从未出现过 -1
or
或者
function arrayContains(needle, arrhaystack)
{
return (arrhaystack.indexOf(needle) > -1);
}
It's worth noting that array.indexOf(..)
is not supported in IE < 9, but jQuery's indexOf(...)
function will work even for those older versions.
值得注意的array.indexOf(..)
是IE < 9 不支持,但 jQuery 的indexOf(...)
功能即使对于那些旧版本也能工作。
回答by lonesomeday
jQuery offers $.inArray
:
jQuery 提供$.inArray
:
Note that inArray returns the index of the element found, so 0
indicates the element is the first in the array. -1
indicates the element was not found.
请注意, inArray 返回找到的元素的索引,因此0
表示该元素是数组中的第一个。-1
表示未找到该元素。
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = $.inArray('specialword', categoriesPresent) > -1;
var foundNotPresent = $.inArray('specialword', categoriesNotPresent) > -1;
console.log(foundPresent, foundNotPresent); // true false
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Edit 3.5 years later
3.5年后编辑
$.inArray
is effectively a wrapper for Array.prototype.indexOf
in browsers that support it (almost all of them these days), while providing a shim in those that don't. It is essentially equivalent to adding a shim to Array.prototype
, which is a more idiomatic/JSish way of doing things. MDN provides such code. These days I would take this option, rather than using the jQuery wrapper.
$.inArray
实际上是Array.prototype.indexOf
支持它的浏览器的包装器(现在几乎所有浏览器),同时在那些不支持的浏览器中提供垫片。它本质上等同于向 中添加一个 shim Array.prototype
,这是一种更惯用的/JSish 做事方式。MDN 提供了这样的代码。这些天我会选择这个选项,而不是使用 jQuery 包装器。
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = categoriesPresent.indexOf('specialword') > -1;
var foundNotPresent = categoriesNotPresent.indexOf('specialword') > -1;
console.log(foundPresent, foundNotPresent); // true false
Edit another 3 years later
3年后再编辑
Gosh, 6.5 years?!
天哪,6.5 年?!
The best option for this in modern Javascript is Array.prototype.includes
:
在现代 Javascript 中,最好的选择是Array.prototype.includes
:
var found = categories.includes('specialword');
No comparisons and no confusing -1
results. It does what we want: it returns true
or false
. For older browsers it's polyfillable using the code at MDN.
没有比较,也没有令人困惑的-1
结果。它做我们想要的:它返回true
or false
。对于较旧的浏览器,它可以使用 MDN 上的代码进行 polyfill。
var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];
var foundPresent = categoriesPresent.includes('specialword');
var foundNotPresent = categoriesNotPresent.includes('specialword');
console.log(foundPresent, foundNotPresent); // true false
回答by ?ime Vidas
Here you go:
干得好:
$.inArray('specialword', arr)
This function returns a positive integer (the array index of the given value), or -1
if the given value was not found in the array.
此函数返回一个正整数(给定值的数组索引),或者-1
如果在数组中找不到给定值。
Live demo:http://jsfiddle.net/simevidas/5Gdfc/
现场演示:http : //jsfiddle.net/simevidas/5Gdfc/
You probably want to use this like so:
你可能想像这样使用它:
if ( $.inArray('specialword', arr) > -1 ) {
// the value is in the array
}
回答by JaredPar
You can use a for
loop:
您可以使用for
循环:
var found = false;
for (var i = 0; i < categories.length && !found; i++) {
if (categories[i] === "specialword") {
found = true;
break;
}
}
回答by Adam Eberlin
I don't like $.inArray(..)
, it's the kind of ugly, jQuery-ish solution that most sane people wouldn't tolerate. Here's a snippet which adds a simple contains(str)
method to your arsenal:
我不喜欢$.inArray(..)
,这是大多数理智的人不会容忍的那种丑陋的、jQuery 风格的解决方案。这是一个片段,它contains(str)
为您的武器库添加了一个简单的方法:
$.fn.contains = function (target) {
var result = null;
$(this).each(function (index, item) {
if (item === target) {
result = item;
}
});
return result ? result : false;
}
Similarly, you could wrap $.inArray
in an extension:
同样,您可以包装$.inArray
在扩展名中:
$.fn.contains = function (target) {
return ($.inArray(target, this) > -1);
}