2017-02-23 111 views
-1

我申請基礎上,選擇單選按鈕,過濾器如何使它更有效,而無需使用if條件如何以有效的方式編寫以下代碼?

applyExtraFilter() { 
     this.moreFilter = !this.moreFilter; 
     this.allItems = this.listings; 
     if (this.deliveryConcession[0].checked) { 
      this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.deliveryConcession[0].checked); 
     } 
     if (this.deliveryConcession[1].checked) { 
      this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.deliveryConcession[2].checked); 
     } 
     if (this.deliveryConcession[2].checked) { 
      this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.deliveryConcession[3].checked); 
     } 
     if (this.seatConcession[0].checked) { 
      this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.seatConcession[0].checked); 
     } 
     if (this.seatConcession[1].checked) { 
      this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.seatConcession[1].checked); 
     } 
     if (this.seatConcession[2].checked) { 
      this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == this.seatConcession[3].checked); 
     } 
     this.setPage(1); 
    } 
+0

你明確訪問'this.deliveryConcession'項而不是迭代它的任何具體原因? –

+0

實際上沒有具體的原因 – user2280016

+0

我有一個解決方案,但我意識到你只是在第一種情況下迭代'[0,2,3]'而在第二種情況下'[0,1,3]' 。 –

回答

1

可以使用forEach()功能。

注意:這將是更容易,如果你會遍歷this.deliveryConcessionthis.seatConcession變量的所有元素。就你而言,我必須在每個循環中添加有關索引的額外條件。

applyExtraFilter() { 
 
    this.moreFilter = !this.moreFilter; 
 
    this.allItems = this.listings; 
 

 
    this.deliveryConcession.forEach((v, i) => v.checked && (i == 0 || i == 2 || i == 3) ? this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == v.checked) : v); 
 

 
    this.seatConcession.forEach((v, i) => v.checked && (i == 0 || i == 1 || i == 3) ? this.allItems = this.allItems.filter(fil => fil.seatingConcession.parking == v.checked) : v); 
 

 
    this.setPage(1); 
 
}

0

這將只過濾一次,就可以肯定地做了更多的改進。

applyExtraFilter() { 
    this.moreFilter = !this.moreFilter; 
    this.allItems = this.listings; 
    this.allItems = this.allItems.filter(fil => { 
     let result = true; 
     if (this.deliveryConcession[0].checked && fil.seatingConcession.parking != this.deliveryConcession[0].checked) { 
      result = false; 
     } 
     if (this.deliveryConcession[1].checked && fil.seatingConcession.parking != this.deliveryConcession[2].checked) { 
      result = false; 
     } 
     if (this.deliveryConcession[2].checked && fil.seatingConcession.parking != this.deliveryConcession[3].checked) { 
      result = false; 
     } 
     if (this.seatConcession[0].checked && fil.seatingConcession.parking != this.seatConcession[0].checked) { 
      result = false; 
     } 
     if (this.seatConcession[1].checked && fil.seatingConcession.parking != this.seatConcession[1].checked) { 
      result = false; 
     } 
     if (this.seatConcession[2].checked && fil.seatingConcession.parking != this.seatConcession[3].checked) { 
      result = false; 
     } 

     return result; 
    }); 
    this.setPage(1); 
    } 

我已經使用let聲明result,如果該關鍵字讓你與你的IDE使用var,而不是問題。

相關問題