Alternative to multiple if else statements in JavaScript?

大兔子大兔子 提交于 2020-06-25 06:37:08

问题


I have an input which contains a dropdown list of 8 items. Depending on the option the user picks, I want to change the value of their input into a a different string value. In order to do this, I am using a ton of if else statements, which make this look very bulky and I would like to condense this if at all possible. I have the following code:

if (inputFive == "Corporation"){
    inputFive = "534"
} else if (inputFive == "LLC"){
    inputFive = "535"
} else if(inputFive == "LLP"){
    inputFive = "536"
} else if(inputFive == "Partnership"){
    inputFive = "537"
} else if(inputFive == "Sole Proprietorship"){
    inputFive = "538"
} else if(inputFive == "Limited Partnership"){
    inputFive = "539"
} else {
    inputFive = "540"
}

As you can see, this looks a little old-school, and I would like to see if there is a better/simpler way to make this happen. Just looking to condense this code if at all possible. I believe that their might be a way to create a dictionary by assigning key/value objects, but I don't know how to do this correctly... All options/hints will be appreciated!


回答1:


You can use an object as a map:

function getCode(input) {
    var inputMap = {
      "Corporation": "534",
      "LLC": "535",
      "LLP": "536",
      "Partnership": "537",
      "Sole Proprietorship": "538",
      "Limited Partnership": "539"
    };

    var defaultCode = "540";
    
    return inputMap[input] || defaultCode;
}

console.log(getCode("LLP"));
console.log(getCode("Lorem Ipsum"));



回答2:


Your intuition is exactly right. You would do it like so:

var mapping = {
    "Corporation": "534",
    "LLC": "535",
    ...
    "default": "540"
}
inputFive = mapping[inputFive] || mapping["default"]



回答3:


Use the switch statement, which is better for times when there is a single variable you want to check against multiple possible values:

switch (inputFive) {
  case "Corporation" :
    inputFive = "534";
    break;
  case "LLC":
    inputFive = "535";
    break;
  case "LLP":
    inputFive = "536";
    break;
  case "Partnership":
    inputFive = "537";
    break;
  case "Sole Proprietorship":
    inputFive = "538";
    break;
  case "Limited Partnership":
    inputFive = "539";
    break;
  default:
    inputFive = "540";
    break;
}



回答4:


   // just another way but not faster than others at all
   // so then order is important 
   var inputMap = [
          "Corporation","LLC","LLP",
          "Partnership","Sole Proprietorship",
          "Limited Partnership"
        ];
    var res = inputMap.indexOf(input);
    res = res > -1 ? 534 + res : 540;



回答5:


You probably want some kind of array.

businessTypes = [];
businessTypes["Corporation"] = 534;
businessTypes["LLC"] = 535;
businessTypes["LLP"] = 536;
businessTypes["Partnership"] = 537;
businessTypes["Sole Proprietorship"] = 538;
businessTypes["Limited Partnership"] = 539;

Then you could reference it with something like:

businessId = businessTypes[inputFive] ? businessTypes[inputFive] : 540;
console.log(businessId);

You could also break it into a function:

function getBusinessId(type) {
  businessTypes = [];
  businessTypes["Corporation"] = 534;
  businessTypes["LLC"] = 535;
  businessTypes["LLP"] = 536;
  businessTypes["Partnership"] = 537;
  businessTypes["Sole Proprietorship"] = 538;
  businessTypes["Limited Partnership"] = 539;
  return businessTypes[type] ? businessTypes[type] : 540;
}

var businessId = getBusinessId("LLC");
console.log(businessId); // 535


来源:https://stackoverflow.com/questions/44977953/alternative-to-multiple-if-else-statements-in-javascript

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