簡體   English   中英

如何在 JavaScript/jQuery 中查找數組是否包含特定字符串?

[英]How to find if an array contains a specific string in JavaScript/jQuery?

有人能告訴我如何檢測"specialword"是否出現在數組中嗎? 例子:

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

你真的不需要 jQuery 。

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

提示:indexOf 返回一個數字,表示指定的搜索值第一次出現的 position,如果從未出現,則返回 -1

或者

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

值得注意的是, IE < 9 不支持array.indexOf(..) ,但 jQuery 的indexOf(...) function 甚至適用於那些舊版本。

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年后編輯

$.inArrayArray.prototype.indexOf在支持它的瀏覽器中的包裝器(現在幾乎所有瀏覽器),同時在那些不支持它的瀏覽器中提供一個 shim。 它本質上等同於向Array.prototype添加 shim,這是一種更慣用/JSish 的做事方式。 MDN 提供了這樣的代碼 這些天我會選擇這個選項,而不是使用 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 對於較舊的瀏覽器,它可以使用 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

這里是 go:

$.inArray('specialword', arr)

此 function 返回正值 integer(給定值的數組索引),如果在數組中未找到給定值,則返回-1

現場演示: http://jsfiddle.net/simevidas/5Gdfc/

您可能希望像這樣使用它:

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

您可以使用for循環:

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

我不喜歡$.inArray(..) ,這是一種丑陋的 jQuery 式解決方案,大多數理智的人都不會容忍。 這是一個片段,它為您的武器庫添加了一個簡單的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);
}

使用現代 javascript 的 Array 方法:

Array.prototype.includes() // 在 ES7 中引入:

  • 返回 boolean

 const data = { categories: [ "specialword", "word1", "word2" ] } console.log("Array.prototype.includes()") // Array.prototype.includes() // returns boolean console.log(data.categories.includes("specialword")) console.log(data.categories.includes("non-exist"))
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Array.prototype.find() // 在 ES6 中引入:

  • 返回找到的元素或未定義的

 const data = { categories: [ "specialword", "word1", "word2" ] } console.log("Array.prototype.find()") // Array.prototype.find() // returns the element if found // returns undefined if not found console.log(data.categories.find(el => el === "specialword").= undefined) console.log(data.categories.find(el => el === "non-exist") != undefined)
 .as-console-wrapper { max-height: 100%;important: top; 0; }

 var myarr = ["I", "like", "turtles"]; isVal = myarr.includes('like') console.log(isVal)

請檢查這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM