vendredi 7 juillet 2017

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!

Aucun commentaire:

Enregistrer un commentaire