[removed] compare variable against array of values

前端 未结 5 877
滥情空心
滥情空心 2021-02-06 07:12

In javascript I am doing the following which works fine.

if (myVar == 25 || myVar == 26 || myVar == 27 || myVar == 28)
 {
   //do something
 }

5条回答
  •  醉话见心
    2021-02-06 07:14

    Since indexOf(), has some browser compatibility issues and requires an extra step (of comparing the result to -1), an alternative, cross-browser approach is the following jQuery utility method (if you include jQuery in your project) :

    if($.inArray(myVar, [25, 26, 27, 28]) > -1) {
         // ... do something ... 
    }
    

提交回复
热议问题