categories: [
"specialword"
"word1"
"word2"
]
1163064 次浏览

在这里你去:

$.inArray('specialword', arr)

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

现场演示:http://jsfiddle.net/simevidas/5Gdfc/

你可能想这样使用:

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

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>


Edit 3.5 years later

$.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. It is essentially equivalent to adding a shim to Array.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.

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

您可以使用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);
}

你真的不需要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(..)IE<9不支持,但jQuery的indexOf(...)函数甚至可以用于那些旧版本。

使用现代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() // introduced in ES6:

  • returns found element or undefined

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

我们可以使用包括选项(这是js内置函数),如果找到值,它将返回true,否则它将是false。

如果你想要精确的索引,你可以使用indexOf(这也是js的内置函数),如果找到值,它将返回精确的索引,否则它将返回-1。

您可以使用返回布尔值的.一些方法切换.包括。 它将在找到匹配项后立即退出,这对于大型数组的性能非常好:

注:全部区分大小写

var myarr = ["I", "like", "turtles"];


isVal = myarr.includes('like')
index = myarr.indexOf('like')
some = myarr.some(item => item.toLowerCase() == 'like'.toLowerCase())




console.log(isVal)
console.log(index)
console.log(some)

请检查这个。