在jquery中将值与JSON数组值匹配

时间:2022-10-26 10:55:51

I have a JSON array that looks like this:

我有一个看起来像这样的JSON数组:

['Monkey','Cheetah','Elephant','Lizard','Spider']

I also have a text input. I want to test whether the value of the input upon 'blur' is also in the array and if it is do something.

我也有文字输入。我想测试'blur'上的输入值是否也在数组中以及它是否有效。

Knowing a bit of python I tried something like this:

知道了一点python我试过这样的事情:

var existing_animals = ['Monkey','Cheetah','Elephant','Lizard','Spider']
$('input').blur(function() {
  user_animal = $(this).val()
  if (user_animal in existing_animals) {
    alert('Animal already exists!')
  }
});

So, how rookie is my mistake?

那么,菜鸟是怎么回事?

4 个解决方案

#1


6  

The in operator checks if a key is present in a dictionary (object). It however does now work for arrays. The right approach is to use jQuery.inArray:

in运算符检查字典(对象)中是否存在键。但它确实适用于数组。正确的方法是使用jQuery.inArray:

if ($.inArray(user_animal, existing) > -1) {
    alert('Animal already exists!')
}

#2


4  

in is for checking if an object includes the attribute, where-as you're using an array.

in用于检查对象是否包含属性,在您使用数组时。

If you want to use your in, you can do:

如果你想使用你的in,你可以这样做:

var existing_animals = {
  "Monkey":1,
  "Cheetah":1
};

etc, then all will be fine using in, however, it would be better to continue using your Array, and simply loop over it.

等等,然后一切都会很好用,但是,最好继续使用你的数组,然后简单地循环它。

$('input').blur(function() {
  user_animal = $(this).val()

  for (var i=0;i<existing_animals.length;i++) {
     if (existing_animals[i] == user_animal) {
        alert("Animal exists");

        break;
     };
  };
});

The ECMA 5th edition introduces an indexOf method for Arrays, so it should be as easy as doing:

ECMA第5版为数组引入了一个indexOf方法,所以它应该像下面这样简单:

if (existing_animals.indexOf(user_animal) != -1) {
   alert("Animal exists");
};

However, IE doesn't support this; you can implement it yourself though.

但是,IE不支持这一点;你可以自己实现它。

#3


0  

If you want to use the "in" keyword in JS, this might be helpful: Reduce multiple ORs in IF statement in Javascript

如果你想在JS中使用“in”关键字,这可能会有所帮助:在Javascript中减少IF语句中的多个OR

#4


0  

I believe you might find your answer in this article.

我相信你可能会在这篇文章中找到答案。

Since the JSON evaluates to an array, you should be looking at the indexOf function. Not sure why you mentioned python here..

由于JSON求值为数组,因此您应该查看indexOf函数。不知道为什么你在这里提到python ..

#1


6  

The in operator checks if a key is present in a dictionary (object). It however does now work for arrays. The right approach is to use jQuery.inArray:

in运算符检查字典(对象)中是否存在键。但它确实适用于数组。正确的方法是使用jQuery.inArray:

if ($.inArray(user_animal, existing) > -1) {
    alert('Animal already exists!')
}

#2


4  

in is for checking if an object includes the attribute, where-as you're using an array.

in用于检查对象是否包含属性,在您使用数组时。

If you want to use your in, you can do:

如果你想使用你的in,你可以这样做:

var existing_animals = {
  "Monkey":1,
  "Cheetah":1
};

etc, then all will be fine using in, however, it would be better to continue using your Array, and simply loop over it.

等等,然后一切都会很好用,但是,最好继续使用你的数组,然后简单地循环它。

$('input').blur(function() {
  user_animal = $(this).val()

  for (var i=0;i<existing_animals.length;i++) {
     if (existing_animals[i] == user_animal) {
        alert("Animal exists");

        break;
     };
  };
});

The ECMA 5th edition introduces an indexOf method for Arrays, so it should be as easy as doing:

ECMA第5版为数组引入了一个indexOf方法,所以它应该像下面这样简单:

if (existing_animals.indexOf(user_animal) != -1) {
   alert("Animal exists");
};

However, IE doesn't support this; you can implement it yourself though.

但是,IE不支持这一点;你可以自己实现它。

#3


0  

If you want to use the "in" keyword in JS, this might be helpful: Reduce multiple ORs in IF statement in Javascript

如果你想在JS中使用“in”关键字,这可能会有所帮助:在Javascript中减少IF语句中的多个OR

#4


0  

I believe you might find your answer in this article.

我相信你可能会在这篇文章中找到答案。

Since the JSON evaluates to an array, you should be looking at the indexOf function. Not sure why you mentioned python here..

由于JSON求值为数组,因此您应该查看indexOf函数。不知道为什么你在这里提到python ..