Convert from if else to switch statement

I have the following if, else if, else construct and I'm just wondering how I can convert such a construct to 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, 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 
}
+3
source share
6 answers

Your sample code cannot be easily converted to a switch statement in most languages, nor should it. switchdesigned to compare a single variable with a range of constant values, while your logic requires comparison with inconsistent values, without the variable with which they are compared. if/ else ifis the right design for your case.

+6

:

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

if/else

+2

, , ​​ ...

: 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;
    }
  }
};
+2

, /else , .

, , , - :

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

: . ( .) , (, int) /case.

0

, , .

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;
}
0

Source: https://habr.com/ru/post/1761731/


All Articles