Fully functional example below (simplified).
Let's say I have a list of 10 possible abbreviations that could be converted to a full name.
It's not large enough that I'd want to do a lookup in a database.
What is the simplest if-else solution?
Here's my imaginary solution: Logically, it would makes sense to compare two arrays such as:
var bar = [foo == sf, foo == ny, foo == la] ? [San Francisco, New York, Los Angeles] : "";
Otherwise, I have a functioning solution below. Can I do any better than that?
bar = "LA";
sf = "SF";
ny = "NY";
la = "LA";
{
if (bar == sf) {
foo = "San Francisco";
} else if (bar == ny) {
foo = "New York";
} else if (bar == la) {
foo = "Los Angeles";
}
document.getElementById("test").innerHTML = foo;
}
<div id="test">Result</div>
Aucun commentaire:
Enregistrer un commentaire