"在" Javascript / jQuery中的语句 [英] "in" statement in Javascript/jQuery
本文介绍了"在" Javascript / jQuery中的语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Javascript或jQuery有时候像Python中的in语句吗?
Does Javascript or jQuery have sometime like the "in" statement in Python?
aaindea - > True
"a" in "dea" -> True
在中搜索这个词是没有希望的:(
Googling for the word in is hopeless :(
推荐答案
它确实中有 运算符
但仅限于对象键:
It does have an in
operator but is restricted to object keys only:
var object = {
a: "foo",
b: "bar"
};
// print ab
for (var key in object) {
print(key);
}
您也可以将它用于这样的支票:
And you may also use it for checks like this one:
if ("a" in object) {
print("Object has a property named a");
}
对于字符串检查,您需要使用 indexOf()方法:
For string checking though you need to use the indexOf() method:
if ("abc".indexOf("a") > -1) {
print("Exists");
}
这篇关于"在" Javascript / jQuery中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文