开发者

Is there a way where I can hold all potential arguments in an array?

开发者 https://www.devze.com 2023-01-16 18:40 出处:网络
Hey again (on a roll today). In jQuery/Javascript is there a way of effectively having this: var myArray = [ \'zero\', \'one\', \'two\', \'three\', 开发者_如何学运维\'four\', \'five\' ];

Hey again (on a roll today).

In jQuery/Javascript is there a way of effectively having this:

var myArray = [ 'zero', 'one', 'two', 'three', 开发者_如何学运维'four', 'five' ];

//get input from user
if (inputFromUser == anythingInArray) {
  alert("it's possible!);
}


is it this?

http://api.jquery.com/jQuery.inArray/


You can use inArray:

var result = $.inArray(inputFromUser, myArray);
if (result >= 0)
{
   alert('Result found at index ' + result);
}


jQuery: $.inArray()

if($.inArray('one', myArray) > -1)  {}

If you need to do it without jQuery, you can use the Array.prototype.indexOf method like

if(myArray.indexOf('one')) {}

That is restricted to ECMAscript Edition 5. If a browser doesn't support that do it the classic route:

for(var i = 0, len = myArray.length; i < len; i++) {
   if(myArray[i] === 'one') {}
}

Ref.: $.inArray()

0

精彩评论

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