[英]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)){
do stuff!
}
Is there any such function in js? 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 ) (参考: 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: JavaScript中没有本机的“ in_array”功能(如PHP),请查看以下解决方案:
http://phpjs.org/functions/in_array:432 http://phpjs.org/functions/in_array:432
Also a search would have lead you here: 另外,搜索也会引导您到这里:
JavaScript equivalent of PHP's in_array() JavaScript等同于PHP的in_array()
if(some_array[category] !== undefined){
// it's there
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.