在IF中优化OR [英] Optimize OR in IF
问题描述
我有这样的代码:
if (action == 'John' || action == 'John Beckham' || action == 'Henry John'){
alert('true!');
}
如何最小化此代码?
应该在IE7中工作.
如果总是出现约翰",最简单的事情是:
if (action.toLowerCase().indexOf("john") !== -1) {
// Do something
}
...但是由于您的问题已经更改了您要针对action
进行一次检查的值,因此我犹豫了一下.还要注意,它将与"xxxjohnxxx"匹配,这可能不是您想要的.
原始建议(已针对您的修改中的新action
值进行了更新)
有很多方法,因为您在评论中都提到过,所以所有情况下都使用大小写 来表示.
String#indexOf
:
if ("|john|john beckham|john henry|giggs john|scholes john|john messi|".indexOf("|" + action.toLowerCase() + "|") !== -1) {
// Do something
}
正则表达式:
if (/^(?:John|John Beckham|John Henry|Giggs John|Scholes John|John Messi)$/i.test(action)) {
// Do something
}
因为您只是使用对/错结果,所以我使用的是
或对象查找: (这还具有让您说出要执行的的优势,例如 或者(在启用ES5的环境中或使用ES5填充板的情况下) 或由于您使用jQuery,因此可以通过使用 I have a code like this: How do I minimize this code? Should work in IE7. If "John" always appears, the simplest thing is: ...but as your question has already changed the values against which you're checking Original suggestions (updated for new There are lots of ways, all shown using case insensitivity since you mentioned that in the comments:
Regular expressions: Because you're just using the true/false result, I'm using Or a
Or an object lookup: (That also has the advantage of letting you say what to do — e.g., the Or (on an ES5-enabled environment or with an ES5 shim) or since you use jQuery, you can avoid the shim on older browsers by using
这篇关于在IF中优化OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!test
仅返回true/false,而不是
var actions = {
"john": true,
"john beckham": true,
"john henry": true,
"giggs john": true,
"scholes john": true,
"john messi": true
};
if (actions[action.toLowerCase()]) {
// do something
}
true
可以用您调用的函数代替.)
Array#indexOf
:if (["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"].indexOf(action.toLowerCase()) !== -1 {
// Do something
}
inArray
:if ($.inArray(action.toLowerCase(), ["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"]) !== -1) {
// Do something
}
if (action == 'John' || action == 'John Beckham' || action == 'Henry John'){
alert('true!');
}
if (action.toLowerCase().indexOf("john") !== -1) {
// Do something
}
action
once, I hesitate to assume that. Also note that it will match "xxxjohnxxx", which may not be what you want.action
values from your edit):
String#indexOf
:if ("|john|john beckham|john henry|giggs john|scholes john|john messi|".indexOf("|" + action.toLowerCase() + "|") !== -1) {
// Do something
}
if (/^(?:John|John Beckham|John Henry|Giggs John|Scholes John|John Messi)$/i.test(action)) {
// Do something
}
test
which just returns true/false, instead of exec
which returns matching results. Both work in this case, but the browser may be able to ever-so-slightly optimize test
(but then, regex is unlikely to be the best solution if your goal is the fastest result or the least memory use).
switch
:switch (action.toLowerCase()) {
case "john":
case "john beckham":
case "john henry":
case "giggs john":
case "scholes john":
case "john messi":
// Do something
}
var actions = {
"john": true,
"john beckham": true,
"john henry": true,
"giggs john": true,
"scholes john": true,
"john messi": true
};
if (actions[action.toLowerCase()]) {
// do something
}
true
could be replaced with a function you call.)
Array#indexOf
:if (["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"].indexOf(action.toLowerCase()) !== -1 {
// Do something
}
inArray
:if ($.inArray(action.toLowerCase(), ["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"]) !== -1) {
// Do something
}