首页 文章

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

提问于
浏览
462

这个问题在这里已有答案:

有人能告诉我如何检测 "specialword" 是否出现在数组中?例:

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

5 回答

  • 14

    你真的不需要jQuery .

    var myarr = ["I", "like", "turtles"];
    var arraycontainsturtles = (myarr.indexOf("turtles") > -1);
    

    提示:indexOf返回一个数字,表示第一次发生指定搜索值的位置,如果从未发生,则返回-1

    要么

    function arrayContains(needle, arrhaystack)
    {
        return (arrhaystack.indexOf(needle) > -1);
    }
    

    值得注意的是 array.indexOf(..)not supported in IE < 9,但是jQuery的 indexOf(...) 函数甚至可以用于那些旧版本 .

  • 548

    jQuery提供$.inArray

    请注意,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>
    

    3.5年后编辑

    $.inArray 实际上是支持它的浏览器中的 Array.prototype.indexOf 的包装器(现在几乎全部都是这样),而在那些不支持的浏览器中提供垫片 . 它本质上相当于为 Array.prototype 添加一个垫片,这是一种更惯用/ JSish的做事方式 . MDN提供such code . 这些天我会采用这个选项,而不是使用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
    

    3年后再编辑

    天哪,6 . 5年?!

    现代Javascript中最好的选择是 Array.prototype.includes

    var found = categories.includes('specialword');
    

    没有比较,也没有混淆 -1 的结果 . 它做我们想要的:它返回 truefalse . 对于旧版浏览器,它是可填充的using the code at MDN .

    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
    
  • 28

    干得好:

    $.inArray('specialword', arr)
    

    此函数返回正整数(给定值的数组索引),如果在数组中找不到给定值,则返回 -1 .

    Live demo: http://jsfiddle.net/simevidas/5Gdfc/

    您可能希望像这样使用它:

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

    您可以使用 for 循环:

    var found = false;
    for (var i = 0; i < categories.length && !found; i++) {
      if (categories[i] === "specialword") {
        found = true;
        break;
      }
    }
    
  • 4

    我不喜欢 $.inArray(..) ,它是否容忍's the kind of ugly, jQuery-ish solution that most sane people wouldn' . 这是一个片段,为您的武器库添加了一个简单的 contains(str) 方法:

    $.fn.contains = function (target) {
      var result = null;
      $(this).each(function (index, item) {
        if (item === target) {
          result = item;
        }
      });
      return result ? result : false;
    }
    

    同样,您可以在扩展名中包装 $.inArray

    $.fn.contains = function (target) {
      return ($.inArray(target, this) > -1);
    }
    

相关问题