如何在JavaScript / jQuery中查找数组是否包含特定字符串? [重复]

本文翻译自: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"
]

#1楼

参考:https://stackoom.com/question/PfAo/如何在JavaScript-jQuery中查找数组是否包含特定字符串-重复


#2楼

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

#3楼

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(...)函数即使在那些较旧的版本中也可以使用。


#4楼

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
}

#5楼

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 . 它完成了我们想要的:返回truefalse 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 


#6楼

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;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值