2016-09-15 41 views
0

在if语句中是否存在“Or”条件的简写,我认为它是愚蠢的,在下面的情况下,“a”变量重复检查所有“或者”条件:JavaScript - if语句中的“Or”的简写

if(a != 'Cancelled' || a != 'Rejected' || a != 'Delivered' || a != 'Confirmed'){ 

    //execute something here 

} 
+0

不是真的,最好的办法可能是,如果可能的使用和或只是一个单一的标志重写条件。 – bmartin

+0

另一个与基本相同的标题:http://stackoverflow.com/questions/11127753/shorthand-for-multiple-or-expressions-in-if-statement在您发布之前进行搜索。 – epascarello

+0

'a!='取消'|| a!='拒绝'|| ......“将永远是真实的,因为如果它是错误的,那么'a'必须都是''Cancelled''和''Rejected''(等等),这是不可能的。 – Frxstrem

回答

3
if (['Cancelled', 'Rejected', 'Delivered', 'Confirmed'].indexOf(a) == -1) { 
    // do stuff here 
} 
+0

伟大的答案! 非常感谢! +1 –