有没有更简单的方法来编写包含 10 个以上条件的 if 语句?

发布于 2024-12-01 08:41:09 字数 378 浏览 0 评论 0原文

这是我的代码:

if (state == 'AZ' || state == 'CO' || state == 'DC' || state == 'IA' || state == 'LA' || state == 'MN' || state == 'NC' || state == 'ND' || state == 'NM' || state == 'NV' || state == 'OR' || state == 'SC' || state == 'TN' || state == 'VA' || state == 'WA' || state == 'WI' || state == 'WY') {}

是否有更简单的方法可以使用数组来编写此代码?这可行,但我希望它更干净!

Here is my code:

if (state == 'AZ' || state == 'CO' || state == 'DC' || state == 'IA' || state == 'LA' || state == 'MN' || state == 'NC' || state == 'ND' || state == 'NM' || state == 'NV' || state == 'OR' || state == 'SC' || state == 'TN' || state == 'VA' || state == 'WA' || state == 'WI' || state == 'WY') {}

Is there an easier way to write this using an array? This works, but I want it to be cleaner!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

画尸师 2024-12-08 08:41:09

您可以使用对象:

if ({AZ:1,CO:1,DC:1,IA:1,LA:1,MN:1,NC:1,ND:1,NM:1,NV:1,OR:1,SC:1,TN:1,VA:1,WA:1,WI:1,WY:1}[state] == 1) {

编辑:

您还可以在字符串中查找字符串:(

if ("AZ,CO,DC,IA,LA,MN,NC,ND,NM,NV,OR,SC,TN,VA,WA,WI,WY".indexOf(state) != -1) {

这当然假设变量包含合理的内容,像 "," 这样的值会给出误报。 )

这恰好比大多数浏览器中的简单比较更快: http://jsperf.com/test-regexp-vs-obj/3

You can use an object:

if ({AZ:1,CO:1,DC:1,IA:1,LA:1,MN:1,NC:1,ND:1,NM:1,NV:1,OR:1,SC:1,TN:1,VA:1,WA:1,WI:1,WY:1}[state] == 1) {

Edit:

You could also look for the string in a string:

if ("AZ,CO,DC,IA,LA,MN,NC,ND,NM,NV,OR,SC,TN,VA,WA,WI,WY".indexOf(state) != -1) {

(This of course assumes that the variable contains something reasonable, a value like "," would give a false positive.)

This happens to be even faster than the plain comparisons in most browsers: http://jsperf.com/test-regexp-vs-obj/3

听你说爱我 2024-12-08 08:41:09

正则表达式:

if(state.match(/^(AZ|CO|DC|IA|LA|MN|NC|ND|NM|NV|OR|SC|TN|VA|WA|WI|WY)$/)){
    //do whatever
}

Regular Expression:

if(state.match(/^(AZ|CO|DC|IA|LA|MN|NC|ND|NM|NV|OR|SC|TN|VA|WA|WI|WY)$/)){
    //do whatever
}
〃温暖了心ぐ 2024-12-08 08:41:09

如果您使用的是 jQuery:

var states = ['AZ' ,'CO' ,'DC' ,'IA' ,'LA' ,'MN' ,'NC' ,'ND' ,'NM' ,'NV' ,'OR' ,'SC' ,'TN' ,'VA' ,'WA' ,'WI' ,'WY'];
if($.inArray('AZ',states)>=0){
    console.log('Hurrah');
}

如果您没有使用 jQuery,则需要自己的函数,例如 此问题的最佳答案

If you're using jQuery:

var states = ['AZ' ,'CO' ,'DC' ,'IA' ,'LA' ,'MN' ,'NC' ,'ND' ,'NM' ,'NV' ,'OR' ,'SC' ,'TN' ,'VA' ,'WA' ,'WI' ,'WY'];
if($.inArray('AZ',states)>=0){
    console.log('Hurrah');
}

If you're not you'll need your own function like the top answer to this question.

淡淡绿茶香 2024-12-08 08:41:09

将所有可能性放入一个数组中,并使用 indexOf 来检查是否包含在其中。

示例:

if(['NA', 'NB', 'NC'].indexOf(state) > -1)
{
    // true
}

或者添加 contains 并使用它:

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

if(['NA', 'NB', 'NC'].contains(state))
{
    // true
}

Push all your possibilities into an array and use indexOf to check is contained in.

Example:

if(['NA', 'NB', 'NC'].indexOf(state) > -1)
{
    // true
}

Or add contains and use it :

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

if(['NA', 'NB', 'NC'].contains(state))
{
    // true
}
不回头走下去 2024-12-08 08:41:09

Hava 看看这个包含函数的 JavaScript 版本。
只需用您的状态代码填充一个数组,然后使用函数

http://css 进行检查-tricks.com/snippets/javascript/javascript-array-contains/

Hava at look at this javascript version of a contains function.
Simply populate an array with your state codes and check using the function

http://css-tricks.com/snippets/javascript/javascript-array-contains/

忘羡 2024-12-08 08:41:09
if ( ['AZ', 'CO', 'DC', 'And so on'].indexOf(state) != -1 ) {
    // do something
}

注意: IE indexOf 不支持9. 如果您必须支持这些浏览器,您可以使用 jQuery:

if ( $.inArray(state, 'AZ', 'CO', 'DC', 'And so on') != -1 ) {
    // do something
}

手动扩展数组原型

if ( ['AZ', 'CO', 'DC', 'And so on'].indexOf(state) != -1 ) {
    // do something
}

NOTE: indexOf is not supported in IE < 9. If you have to support those browsers, you can either use jQuery:

if ( $.inArray(state, 'AZ', 'CO', 'DC', 'And so on') != -1 ) {
    // do something
}

or manually extend the array prototype.

撩发小公举 2024-12-08 08:41:09
var states=new Array("AZ", "CO", ...);
for (s in states) {
  if (state == s) {
    // do something
    break;
  }
}

是实现这一目标的一种方法。

var states=new Array("AZ", "CO", ...);
for (s in states) {
  if (state == s) {
    // do something
    break;
  }
}

is one way to approach it.

瑾夏年华 2024-12-08 08:41:09

如果您使用 CoffeeScript,它会生成 JavaScript 输出,您可以简单地编写

if state in ['AZ', 'CO', 'DC', 'IA', 'LA', 'MN', 'NC', 'ND', 'NM', 'NV', 'OR', 'SC', 'TN', 'VA', 'WA', 'WI', 'WY']
    ...

If you use CoffeeScript, which generates JavaScript output, you can simply write

if state in ['AZ', 'CO', 'DC', 'IA', 'LA', 'MN', 'NC', 'ND', 'NM', 'NV', 'OR', 'SC', 'TN', 'VA', 'WA', 'WI', 'WY']
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文