jQuery.inArray(),如何正确使用它?

本文翻译自:jQuery.inArray(), how to use it right?

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. 如果对象在数组中,它将返回0,但是Javascript中的0为false。 So the following will output: "is NOT in array" 因此,将输出以下内容: “不在数组中”

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. 他们期望当“ test”在数组中时,jQuery.inArray(“ test”,myarray)为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. 但是一定有充分的理由这样做。


#1楼

参考:https://stackoom.com/question/1HAK7/jQuery-inArray-如何正确使用它


#2楼

The inArray function 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 inArray returns 0 it is indicating that the first argument was found at the first position of the supplied array. inArray返回0 ,表示在提供的数组的第一个位置找到了第一个参数。

To use inArray within an if statement use: 要在if语句中使用inArray ,请使用:

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

inArray returns -1 when the first argument passed to the function is not found in the array passed as the second argument. 当作为第二个参数传递的数组中找不到传递给函数的第一个参数时, inArray返回-1


#3楼

$.inArray returns the index of the element if found or -1 if it isn't -- not a boolean value. $.inArray返回元素的索引 (如果找到),否则返回-1-不是布尔值。 So the correct is 所以正确的是

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

#4楼

jQuery.inArray() returns index of the item in the array, or -1 if item was not found. jQuery.inArray()返回数组中该项的索引;如果未找到该项,则返回-1。 Read more here: jQuery.inArray() 在这里阅读更多: jQuery.inArray()


#5楼

It will return the index of the item in the array. 它将返回数组中该项的索引。 If it's not found you will get -1 如果找不到,您将获得-1


#6楼

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,而不是结果为true还是false。

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. $ .inArray()方法类似于JavaScript的本机.indexOf()方法,因为它找不到匹配项时返回-1。 If the first element within the array matches value, $.inArray() returns 0. 如果数组中的第一个元素与value匹配,则$ .inArray()返回0。

Because JavaScript treats 0 as loosely equal to false (ie 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. 因为JavaScript将0视为宽松地等于false(即0 == false,但0!== false),所以如果我们要检查数组中是否存在值,则需要检查它是否等于(或大于) -1。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值