jQuery.inArray(),怎么用才对?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18867599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:39:04  来源:igfitidea点击:

jQuery.inArray(), how to use it right?

javascriptjquery

提问by nbar

First time I work with jQuery.inArray()and it acts kinda strange.

我第一次jQuery.inArray()与它合作,它的行为有点奇怪。

If the object is in the array, it will return 0, but 0 is false in Javascript. So the following will output: "is NOT in array"

如果对象在数组中,它将返回 0,但 0 在 Javascript 中为 false。因此将输出以下内容:“不在数组中”

var myarray = [];
myarray.push("test");

if(jQuery.inArray("test", myarray)) {
    console.log("is in array");
} else {
    console.log("is NOT in array");
}

I will have to change the if statement to:

我将不得不将 if 语句更改为:

if(jQuery.inArray("test", myarray)==0)

But this makes the code unreadable. Especially for someone who doesn't know this function. They will expect that jQuery.inArray("test", myarray) gives true when "test" is in the array.

但这使得代码不可读。特别是对于不知道此功能的人。他们会期望 jQuery.inArray("test", myarray) 当“test”在数组中时给出 true。

So my question is, why is it done this way? I realy dislike this. But there must be a good reason to do it like that.

所以我的问题是,为什么要这样做?我真的很不喜欢这个。但必须有充分的理由这样做。

回答by Dennis

inArrayreturns the index of the element in the array, not a boolean indicating if the item exists in the array. If the element was not found, -1will be returned.

inArray返回数组中元素的索引,而不是指示该项目是否存在于数组中的布尔值。如果未找到该元素,-1将返回。

So, to check if an item is in the array, use:

因此,要检查一个项目是否在数组中,请使用:

if(jQuery.inArray("test", myarray) !== -1)

回答by Jon

$.inArrayreturns the indexof the element if found or -1 if it isn't -- not a boolean value. So the correct is

$.inArray如果找到则返回元素的索引,否则返回-1 —— 不是布尔值。所以正确的是

if(jQuery.inArray("test", myarray) != -1) {
    console.log("is in array");
} else {
    console.log("is NOT in array");
} 

回答by Patsy Issa

The answer comes from the first paragraph of the documentation check if the results is greater than -1, not if it's true or false.

答案来自文档检查结果是否大于 -1 的第一段,而不是它是对还是错。

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

$.inArray() 方法类似于 JavaScript 的原生 .indexOf() 方法,因为它在找不到匹配项时返回 -1。如果数组中的第一个元素与 value 匹配,则 $.inArray() 返回 0。

因为 JavaScript 将 0 视为大致等于 false(即 0 == false,但 0 !== false),如果我们检查数组中是否存在值,我们需要检查它是否不等于(或大于) ) -1。

回答by 6502

The right way of using inArray(x, arr)is not using it at all, and using instead arr.indexOf(x).

使用正确的方法inArray(x, arr)不使用它在所有的,并使用替代arr.indexOf(x)

The official standard name is also more clear on the fact that the returned value is an index thus if the element passed is the first one you will get back a 0(that is falsy in Javascript).

官方标准名称也更加明确,因为返回值是一个索引,因此如果传递的元素是第一个,您将返回 a 0(在 Javascript 中是假的)。

(Note that arr.indexOf(x)is not supported in Internet Explorer until IE9, so if you need to support IE8 and earlier, this will not work, and the jQuery function is a better alternative.)

(请注意,arr.indexOf(x)不是在Internet Explorer支持IE9之前,因此,如果您需要支持IE8和更早版本,这将无法工作,而jQuery函数是一个更好的选择。)

回答by Jason

Or if you want to get a bit fancy you can use the bitwise not (~) and logical not(!) operators to convert the result of the inArray function to a boolean value.

或者,如果您想变得有点花哨,您可以使用按位非 (~) 和逻辑非 (!) 运算符将 inArray 函数的结果转换为布尔值。

if(!!~jQuery.inArray("test", myarray)) {
    console.log("is in array");
} else {
    console.log("is NOT in array");
}

回答by DDan

I usually use

我通常使用

if(!(jQuery.inArray("test", myarray) < 0))

or

或者

if(jQuery.inArray("test", myarray) >= 0)

回答by Hassan Amir Khan

jQuery inArray() method is use to search a value in an array and return its index not a Boolean value. And if the value was not found it'll return -1.

jQuery inArray() 方法用于搜索数组中的值并返回其索引而不是布尔值。如果未找到该值,它将返回 -1。

So, to check if a value is present in an array, follow the below practice:

因此,要检查数组中是否存在值,请遵循以下做法:

myArray = new Array("php", "tutor");
if( $.inArray("php", myArray) !== -1 ) {

    alert("found");
}

Reference

参考

回答by Kevin Bowersox

The inArrayfunction returns the index of the object supplied as the first argument to the function in the array supplied as the second argument to the function.

inArray函数返回作为函数的第一个参数提供的对象在作为函数的第二个参数提供的数组中的索引。

When inArrayreturns 0it is indicating that the first argument was found at the first position of the supplied array.

inArray返回0它表明的第一个参数是在所提供的阵列的第一位置上被发现。

To use inArraywithin an if statement use:

inArray在 if 语句中使用,请使用:

if(jQuery.inArray("test", myarray) != -1) {
    console.log("is in array");
} else {
    console.log("is NOT in array");
}

inArrayreturns -1when the first argument passed to the function is not found in the array passed as the second argument.

inArray-1当在作为第二个参数传递的数组中找不到传递给函数的第一个参数时返回。

回答by Saurabh Solanki

instead of using jQuery.inArray()you can also use includesmethod for int array :

jQuery.inArray()除了使用,您还可以使用includesint 数组的方法:

var array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

var pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false

check official post here

在此处查看官方帖子

回答by Gustavo Andrade Ferreira

If we want to check an element is inside a set of elements we can do for example:

如果我们想检查一个元素是否在一组元素中,我们可以这样做:

var checkboxes_checked = $('input[type="checkbox"]:checked');

// Whenever a checkbox or input text is changed
$('input[type="checkbox"], input[type="text"]').change(function() {
    // Checking if the element was an already checked checkbox
    if($.inArray( $(this)[0], checkboxes_checked) !== -1) {
        alert('this checkbox was already checked');
    }
}