JS简称if(a == b || a == c) [英] JS Short for if (a == b || a == c)

查看:695
本文介绍了JS简称if(a == b || a == c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个简短的方法来写这个:

I'm searching for a short way to write this:

    if ( variable == 1 || variable == 2 || variable == 6)

我试过

    if (variable == 1 || 2 || 6)

但它似乎不起作用。有没有一种简单的方法可以做到这一点?

but it doesn't seem to work. Is there an easy way to do this?

推荐答案

你可以把你可能的值放在一个数组中并使用 Array.indexOf

You can do it by putting your possible values in an array and using Array.indexOf:

if ([1,2,6].indexOf(variable) != -1)

这篇关于JS简称if(a == b || a == c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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