2016-09-15 38 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 –