开发者

check if multiple items inArray

开发者 https://www.devze.com 2023-02-23 06:13 出处:网络
Is it possible to check if multiple items are contained within an array using jquery\'s inArray function?

Is it possible to check if multiple items are contained within an array using jquery's inArray function?

if ($.inArray('foo' && 'bar', array)开发者_JAVA技巧 == -1) {
    // Neither foo or bar in array
});

Thanks


With jQuery.inArray, you can (quoting) :

Search for a specified value within an array and return its index (or -1 if not found).

Looking at that documentation page, it doesn't seem you can pass more than one value to that function.


So, why not call that function twice : one time for 'foo', and one time for 'bar' :

if ($.inArray('foo', array) == -1 && $.inArray('bar', array) == -1) {
    // Neither foo or bar in array
}


var arr= ['foo','bar'];
var length = arr.length;
for ( var i = 0 ;  i < length; i++ ) {
  if(jQuery.inArray(arr[i],array) > -1) {
   // do whatever you want.
  }

}


What about?

if (array.join(",").match(/foo|bar/gi).length == 2){
   //
}

or

var find = ["foo","bar"], exp = new RegExp(find.join("|"), "gi");
if (array.join(",").match(exp).length == find.length){
    //
}
0

精彩评论

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

关注公众号