博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在JavaScript / jQuery中查找数组是否包含特定字符串? [重复]
阅读量:2288 次
发布时间:2019-05-09

本文共 4253 字,大约阅读时间需要 14 分钟。

本文翻译自:

This question already has an answer here: 这个问题已经在这里有了答案:

  • 47 answers 47个答案

Can someone tell me how to detect if "specialword" appears in an array? 有人可以告诉我如何检测"specialword"出现在数组中吗? Example: 例:

categories: [    "specialword"    "word1"    "word2"]

#1楼

参考:


#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 , but jQuery's indexOf(...) function will work even for those older versions. 值得注意的是 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: 现场演示: :

You probably want to use this like so: 您可能想要这样使用:

if ( $.inArray('specialword', arr) > -1 ) {    // the value is in the array}

#5楼

jQuery offers : jQuery提供 :

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


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 . 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 . 对于较旧的浏览器,可以 。

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

转载地址:http://ficnb.baihongyu.com/

你可能感兴趣的文章
JSTL
查看>>
el表达式
查看>>
日志 log4j的使用
查看>>
[Linux]虚拟机的安装、Linux的安装和Xshell的安装
查看>>
Linux的文件系统
查看>>
Linux的命令入门
查看>>
机器学习_算法_AdaBoost
查看>>
机器学习_算法_KNN
查看>>
Deep Learning_main
查看>>
Deep Learning_mnist background introduction
查看>>
linux_shell_util
查看>>
Project manage_maven3安装,配置
查看>>
linux_ramdisk妙用
查看>>
project manage_maven_android
查看>>
Foreign Language_english_补语
查看>>
Foreign Language_english_感叹句
查看>>
Foreign Language_english_限定词
查看>>
Foreign Language_english_人称代词&数词
查看>>
Foreign Language_english_从句
查看>>
Foreign Language_english_副词和形容词区别
查看>>