本文翻译自:How to find if an array contains a specific string in JavaScript/jQuery? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
Can someone tell me how to detect if "specialword"
appears in an array? 有人可以告诉我如何检测"specialword"
出现在数组中吗? Example: 例:
categories: [
"specialword"
"word1"
"word2"
]
参考:https://stackoom.com/question/PfAo/如何在JavaScript-jQuery中查找数组是否包含特定字符串-重复
I don't like $.inArray(..)
, it's the kind of ugly, jQuery-ish solution that most sane people wouldn't tolerate. 我不喜欢$.inArray(..)
,这是大多数理智的人所不能接受的那种类似于jQuery的丑陋解决方案。 Here's a snippet which adds a simple contains(str)
method to your arsenal: 以下是向您的军械库添加一个简单的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);
}
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. 值得注意的是IE <9不支持 array.indexOf(..)
,但jQuery的indexOf(...)
函数即使在那些较旧的版本中也可以使用。
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
}
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. 请注意,inArray返回找到的元素的索引,因此0
表示该元素是数组中的第一个元素。 -1
indicates the element was not found. -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. $.inArray
是支持Array.prototype.indexOf
的浏览器的包装器( $.inArray
几乎所有此类浏览器),同时在不支持它的浏览器中提供了填充。 It is essentially equivalent to adding a shim to Array.prototype
, which is a more idiomatic/JSish way of doing things. 从本质上讲,这等效于向Array.prototype
添加垫片,这是一种更加惯用的/ JSish的处理方式。 MDN provides such code . MDN提供了这样的代码 。 These days I would take this option, rather than using the jQuery wrapper. 这些天来,我会采用此选项,而不是使用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 三年后再编辑
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. 没有比较,也没有令人困惑的-1
结果。 It does what we want: it returns true
or false
. 它完成了我们想要的:返回true
或false
。 For older browsers it's polyfillable using the code at MDN . 对于较旧的浏览器,可以使用MDN上的代码进行polyfillable 。
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
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;
}
}