如何在 JavaScript/jQuery 中查找数组是否包含特定字符串?
有人可以告诉我如何检测 "specialword"
是否出现在数组中?例子:
categories: [
"specialword"
"word1"
"word2"
]
Can someone tell me how to detect if "specialword"
appears in an array? Example:
categories: [
"specialword"
"word1"
"word2"
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你确实不需要 jQuery 来做这个。
或
值得注意的是
array.indexOf(..)
是 IE 不支持 < 9,但 jQuery 的indexOf(...)
函数甚至适用于那些旧版本。You really don't need jQuery for this.
or
It's worth noting that
array.indexOf(..)
is not supported in IE < 9, but jQuery'sindexOf(...)
function will work even for those older versions.jQuery 提供
$.inArray
:请注意,inArray 返回的索引找到的元素,因此
0
表示该元素是数组中的第一个元素。-1
表示未找到该元素。3.5 年后编辑
$.inArray
实际上是支持它的浏览器(现在几乎所有浏览器)中Array.prototype.indexOf
的包装器,同时在那些没有的。它本质上相当于向 Array.prototype 添加一个垫片,这是一种更惯用/JSish 的做事方式。 MDN 提供了此类代码。现在我会选择这个选项,而不是使用 jQuery 包装器。3 年后再次编辑
天哪,6.5 年?!
在现代 JavaScript 中,最好的选择是
Array.prototype.includes()
:没有比较,没有混淆
-1
结果。它执行我们想要的操作:返回true
或false
。对于旧版浏览器,它是polyfillable,使用代码位于MDN。jQuery offers
$.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.Edit 3.5 years later
$.inArray
is effectively a wrapper forArray.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 toArray.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.Edit another 3 years later
Gosh, 6.5 years?!
The best option for this in modern JavaScript is
Array.prototype.includes()
:No comparisons and no confusing
-1
results. It does what we want: it returnstrue
orfalse
. For older browsers it's polyfillable using the code at MDN.在这里:
此函数返回一个正整数(给定值的数组索引),如果在数组中找不到给定值,则返回 -1 。
现场演示: http://jsfiddle.net/simevidas/5Gdfc/
您可能想使用像这样:
Here you go:
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.Live demo: http://jsfiddle.net/simevidas/5Gdfc/
You probably want to use this like so:
我们可以使用 includes 选项(这是 js 内置函数),如果找到该值将返回 true,否则将返回 false。
如果你想要精确的索引,你可以使用indexOf(这也是js内置函数),如果找到该值,它将返回精确的索引,否则它将返回-1。
您可以使用返回布尔值的 .some 方法来切换 .includes 。
一旦找到匹配,它将立即退出,这对于大型数组的性能非常有用:
注意:全部区分大小写
请检查这个。
we can use includes option (which is js built-in function), which will return true if the value is found else it will be false.
if you want the exact index you can use indexOf (which is also js built-in function), which will return the exact index if the value is found else it will return -1.
You can switch .includes with the .some method which returns a boolean.
It will exit as soon as a match was found, which is great for performance for huge arrays:
Note: all are case sensitive
please check this.
您可以使用
for
循环:You can use a
for
loop:使用现代 javascript 的 Array 方法:
Array.prototype.includes() // ES7 中引入:
Array.prototype.find() // ES6中引入:
With modern javascript's Array methods:
Array.prototype.includes() // introduced in ES7:
Array.prototype.find() // introduced in ES6:
我不喜欢
$.inArray(..)
,它是一种丑陋的、类似 jQuery 的解决方案,大多数理智的人都不会容忍。下面的代码片段将一个简单的contains(str)
方法添加到您的库中:同样,您可以将
$.inArray
包装在扩展中: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 simplecontains(str)
method to your arsenal:Similarly, you could wrap
$.inArray
in an extension: