开发者

Check if Variable in array javascript

Short code for checking if a variable also exists inside an array is needed. Im thinking something like this:

   var category='cars';
   if (in_array(category, some_array)){
    开发者_如何学Python   do stuff!
   }

Is there any such function in js?

Thanks


if (some_array.indexOf(category) >= 0) {
   // do stuff
}

(Ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf)


There's no native "in_array"-function in JavaScript (as in PHP), check out this solution:

http://phpjs.org/functions/in_array:432

Also a search would have lead you here:

JavaScript equivalent of PHP's in_array()


if(some_array[category] !== undefined){
  // it's there
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜