开发者

jQuery.inArray not behaving as expected

I'm trying to loop through a list of numbers and check if that number is part of another list of numbers using jQuery.each and jQuery.inArray. jQuery.inArray does not seem to be behaving as expected.

Here is my code:

var some_numbers = [1, 2];
var more_numbers = [0, 1, 2];

$.each(more_numbers, function(index, value) {
  if($.inArray(value, some_numbers)) {
    console.log(value);
  }
});

console.log('Some Numbers:');
console.log(som开发者_JAVA技巧e_numbers);

Here is the resulting console output:

0
2
Some Numbers:
[1, 2]

Will someone please help? This is maddening.

Edit: Problem solved! Changed my condition to this:

if($.inArray(value, some_numbers) !== -1)

Thanks everyone!


$.inArray returns an index, not a boolean.

To check whether the element was found, you need to check whether it's >= 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜