可能重复: 测试Javascript数组中的值 在JavaScript数组中查找项目的最佳方法? Javascript - array.contains(obj)
我通常在python中编程,但最近开始学习JavaScript。
I usually program in python but have recently started to learn JavaScript.
在python中,这是一个完全有效的if语句:
In python this is a perfectly valid if statement:
list = [1,2,3,4] x = 3 if x in list: print "It's in the list!" else: print "It's not in the list!"但是我在Javascript中做了同样的事情。
but I have had poblems doing the same thing in Javascript.
如何在JavaScript中检查x是否在列表y中?
How do you check if x is in list y in JavaScript?
推荐答案使用 indexOf 。您需要使用该页面上兼容性下列出的代码来添加对未实现该JS版本的浏览器的支持。
Use indexOf which was introduced in JS 1.6. You will need to use the code listed under "Compatibility" on that page to add support for browsers which don't implement that version of JS.
JavaScript确实有在运算符中,但它会测试键而不是值。
JavaScript does have an in operator, but it tests for keys and not values.
更多推荐
如果在x中的Javascript
发布评论