2017-09-13 112 views
1

我只是在尋找一些關於React.js篩選的建議。我目前通過'employeeName'過濾'peopleList',這工作正常,即時回到我期望的。React.js - 篩選多個indexOf

但我想同時過濾'employeeID',即檢查'employeeName'或'employeeID'是否包含indexOf ..這是可能的還是我需要設置兩個過濾器'employeeName'和'employeeID'?

let people= this.state.peopleList.filter(
     (person) => { 
      return person.record.employeeName.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1; 
      // return person.record.employeeID.toLowerCase().indexOf(this.state.search.toLowerCase()) !== -1; 
     } 
    ); 
+0

您是否嘗試過使用''||,*或*操作? 'employeeNameIndexOf!== -1 || employeeIdIndexOf!== -1' –

回答

1

如果你的條件是一個或另一個,你可以使用||操作

const { search, peopleList } = this.state 
const searchStr = search.toLowerCase() 

const people = peopleList.filter((person) => 
    person.record.employeeName.toLowerCase().indexOf(searchStr) !== -1 || 
    person.record.employeeId.indexOf(searchStr) !== -1 
) 
+0

謝謝。這工作很好!我試圖做到這一點,但我沒有正確的結構。 – ballbern