如果在x中的Javascript

编程入门 行业动态 更新时间:2024-10-23 12:35:40
本文介绍了如果在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

本文发布于:2023-10-18 10:50:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!