在IF中优化OR [英] Optimize OR in IF

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

问题描述

我有这样的代码:

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
}

因为您只是使用对/错结果,所以我使用的是 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可以用您调用的函数代替.)


或者(在启用ES5的环境中或使用ES5填充板的情况下)Array#indexOf:

if (["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"].indexOf(action.toLowerCase()) !== -1 {
    // Do something
}

或由于您使用jQuery,因此可以通过使用 inArray :

if ($.inArray(action.toLowerCase(), ["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"]) !== -1) {
    // Do something
}

I have a code like this:

if (action == 'John' || action == 'John Beckham' || action == 'Henry John'){
     alert('true!');
}

How do I minimize this code?

Should work in IE7.

解决方案

If "John" always appears, the simplest thing is:

if (action.toLowerCase().indexOf("john") !== -1) {
    // Do something
}

...but as your question has already changed the values against which you're checking action once, I hesitate to assume that. Also note that it will match "xxxjohnxxx", which may not be what you want.

Original suggestions (updated for new action values from your edit):


There are lots of ways, all shown using case insensitivity since you mentioned that in the comments:

String#indexOf:

if ("|john|john beckham|john henry|giggs john|scholes john|john messi|".indexOf("|" + action.toLowerCase() + "|") !== -1) {
    // Do something
}


Regular expressions:

if (/^(?:John|John Beckham|John Henry|Giggs John|Scholes John|John Messi)$/i.test(action)) {
    // Do something
}

Because you're just using the true/false result, I'm using 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).


Or a switch:

switch (action.toLowerCase()) {
    case "john":
    case "john beckham":
    case "john henry":
    case "giggs john":
    case "scholes john":
    case "john messi":
        // Do something
}


Or an object lookup:

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
}

(That also has the advantage of letting you say what to do — e.g., the true could be replaced with a function you call.)


Or (on an ES5-enabled environment or with an ES5 shim) Array#indexOf:

if (["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"].indexOf(action.toLowerCase()) !== -1 {
    // Do something
}

or since you use jQuery, you can avoid the shim on older browsers by using inArray:

if ($.inArray(action.toLowerCase(), ["john", "john beckham", "john henry", "giggs john", "scholes john", "john messi"]) !== -1) {
    // Do something
}

这篇关于在IF中优化OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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