convert from if else to switch statement

自闭症网瘾萝莉.ら 提交于 2020-01-04 02:50:08

问题


I have the following if, else if, else construct and I am just curious how I could convert such as construct into a switch statement.

var emailSubject = email.subject.toLowerCase(); 
if(emailSubject.indexOf("account request") >= 0){
     //do acct req
}else if(emailSubject.indexOf("accounts pending removal for") >= 0){
     //do account removal 
}else if(emailSubject.indexOf("listserv application") >= 0){
     //do listserv app 
}else if(emailSubject.indexOf("student organization webmaster transfer request") >= 0){
     //do webmaster xfer 
}else{
     //do default 

} 

My thoughts are but I do not think this is correct:

switch(emailSubject){
    case this.indexOf("account request"):
       //do acct request 
       break;
    default:
       //do default 
}

Or

switch(0){
   case emailSubject.indexOf("accounts pending removal"):
     //process account pending removal 
     break;
   default:
     //do default behavior 
}

回答1:


Your example code cannot easily be converted to a switch statement in most languages, nor should it. switch is for comparing a single variable against a range of constant values, whereas your logic requires comparison against non-constant values, with no variable to compare them with. if/else if is the correct construction for your case.




回答2:


You can only use case to check a value:

switch(emailSubject){
    case "Subject1": //(emailSubject == "Subject1")
       //do acct request 
       break;
    case "Subject2": //(emailSubject == "Subject2")
       //do something else
       break;
    default:
       //do default 
}

Otherwise you should be using if/else




回答3:


Constructs like this are usually crying out for polymorphism...

Play with it here: http://jsbin.com/utilu4/3

var mailHandlers = [

  {
    CanHandleEmail : function(email) {
      return email.subject.toLowerCase().indexOf("account request") >= 0;
    },

    HandleEmail : function(email) {
      alert("do acct req");
    }
  },

  {
    CanHandleEmail : function(email) {
      return email.subject.toLowerCase().indexOf("account pending removal for") >= 0;
    },

    HandleEmail : function(email) {
      alert("do account removal");
    }
  },

  {
    CanHandleEmail : function(email) {
      return email.subject.toLowerCase().indexOf("listserv application") >= 0;
    },

    HandleEmail : function(email) {
      alert("do listserv app");
    }
  },

  {
    CanHandleEmail : function(email) {
      return email.subject.toLowerCase().indexOf("student organization webmaster transfer request") >= 0;
    },

    HandleEmail : function(email) {
      alert("do webmaster xfer");
    }
  },

  {
    CanHandleEmail : function(email) {
      return true;
    },

    HandleEmail : function(email) {
      alert("do default");
    }
  }
];

function HandleEmail(email) {
  for(i=0; i< mailHandlers.length; i++) {
    if(mailHandlers[i].CanHandleEmail(email)){
      mailHandlers[i].HandleEmail(email);
      break;
    }
  }
};



回答4:


As mentioned, if/else is best for what you've got.

If, however, you were looking for actual whole subject lines, instead of words within subject lines, you could do something like:

var a = ["account request", "listserv application", "student organization webmaster transfer request"];
switch(a.indexOf(emailSubject)) {
  // ...
}



回答5:


Just as a tip: wrap your code in a function and return the value of the match. (You don't have to use else in this case.) If you want to, you could return a code for the match (eg. an int) and use switch/case to perform the action.




回答6:


I've just come across this in the wild and I can't help but share it, but don't do it.

var emailSubject = email.subject.toLowerCase(); 
switch (true) {
    case (emailSubject.indexOf("account request") >= 0):
        //do acct req
        break;
    case (emailSubject.indexOf("accounts pending removal for") >= 0):
        //do account removal 
        break;
    case (emailSubject.indexOf("listserv application") >= 0):
        //do listserv app 
        break;
    case (emailSubject.indexOf("student organization webmaster transfer request") >= 0):
        //do webmaster xfer 
        break;
    default:
        //do default
        break;
}


来源:https://stackoverflow.com/questions/3575825/convert-from-if-else-to-switch-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!