I have some set of if
conditions for a particular array using JavaScript.
activity
is the array containing 10-12 items. This array is converted to a multiple select dropdown. Depending upon the value selected, I need to hide and show divs. I also need to set values of another input choice as "NA".
Here's the code written for the onchange
of that multiple select dropdown.
activity = ["strategy session","sessions","virtual",...,"Other"]
if (activity.indexOf("strategy session") != -1) {
$("#FoPStrategySession").show();
}
if (activity.indexOf("sessions") != -1) {
$("#acprojectname").show();
if (supportmodel == "Level") {
$(".accombohide").hide();
$("[title='Test']").val("NA");
$("[title='Test2']").val("NA");
}
}
if (activity.indexOf("virtual") != -1) {
if (supportmodel == "Level") {
$(".lvl3_consult").hide();
$("[title='Test']").val("NA");
$("[title='Test2']").val("NA");
}
}
if (activity.indexOf("Other") != -1) {
$("#acactivityother").show();
}
Is there any other way to efficiently write this code using switch case or any other method?
activity
contain multiple types you check for? For example, canactivity = ["sessions", "virtual"]
? \$\endgroup\$activity
contain? What issupportmodel
? If you need to, in essence, detect the presence of the value of "session", "virtual", etc. in the array, then I question whether an object with property names matching the values be used to allow direct O(1) lookup rather than having to iterate an entire array looking for the presence of the value. \$\endgroup\$