如果在x中的Javascript [英] Javascript if in x

查看:170
本文介绍了如果在x中的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

测试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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆