What's the quickest way, from a readability/typing standpoint, to assign a value to a specific variable based on a related variable?
var abbrev;
if(state=='Pennsylvania'){
abbrev='PA';
}else if(state=='New Jersey'){
abbrev='NJ';
}else if(state=='Delaware'){
abbrev='DE';
}
//and so on...
I'm trying to avoid making one array for the state name and a another array for the abbreviation because the relationship is lost with separate declarations.
You could use an object for the abbreviation, like
var abbreviations = {
'Pennsylvania': 'PA',
'New Jersey': 'NJ',
'Delaware': 'DE'
};
Usage:
abbrev = abbreviations[state];
just a suggestion, have you tried using CASE? it looks more clean and readable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With