gpt4 book ai didi

javascript - 缩短条件语句?

转载 作者:行者123 更新时间:2023-11-28 15:40:18 24 4
gpt4 key购买 nike

相当简单的问题,不确定我在这里缺少什么......我有这样的东西:

if(this == 18 || this == 19 || this == 20 || this == 21) { 
...
}

如何缩短它?我尝试过 if(this == 18 || 19 || 20...) 但这不起作用。我还有大约 50 个这样的值需要去处理,所以如果我能做到的话那就太好了。

谢谢。

最佳答案

假设有一个适合旧版浏览器的填充程序:

if( [18,19,20,21].indexOf(this) > -1) {

或者,另一种选择:

if( "|"+[18,19,20,21].join("|")+"|".indexOf("|"+this+"|") > -1) {

关于javascript - 缩短条件语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24004358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com