1. A simple example is as follows:
switch use case
var caseContent = '';//Processing content after conditional judgment
var caseValue = 5;//Conditional judgment value
switch(caseValue){
case 0:
caseContent = "Shoes";
break;
case 1:
caseContent = "Pants";
break;
case 2:
caseContent = "Coat";
break;
... ...
case 5:
caseContent = "hat";
break;
default :
caseContent = "whatever";
break;
}
Array use case
var caseContent = '';//Conditional judgment post-processing content
var caseValue = 5;//Conditional judgment value
var caseContentArr = ["Shoes", "Pants", "Coat"... ... ,"hat"];
caseContent = caseContentArr[caseValue] ? caseContentArr[caseValue]:"whatever";
2. Analysis of advantages and disadvantages Array method The code is concise and efficient, but its readability is not as good as the switch use case.