user7366442
user7366442

Reputation: 751

Alternative to multiple if else statements in JavaScript?

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!

Upvotes: 3

Views: 13113

Answers (5)

Matt
Matt

Reputation: 5428

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

Upvotes: 2

nullqube
nullqube

Reputation: 3009

   // 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;

Upvotes: 2

Alberto Trindade Tavares
Alberto Trindade Tavares

Reputation: 10396

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"));

Upvotes: 16

Scott Marcus
Scott Marcus

Reputation: 65845

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

Upvotes: 4

user1726343
user1726343

Reputation:

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

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

Upvotes: 5

Related Questions