Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js – filter multiple indexOf

Im just looking for a bit of advice regarding React.js filtering. I am currently filtering ‘peopleList’ by ‘employeeName’ and this is working fine, im getting back exactly what I expect.

But I wanted to also filter by the ‘employeeID’ at the same time i.e. check if ‘employeeName’ or ‘employeeID’ contain an indexOf.. Is this possible or would I need to set up two filters for 'employeeName’ and '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;
        }
    );
like image 237
ballbern Avatar asked Apr 06 '26 19:04

ballbern


1 Answers

If your condition is either one OR the other, you can use the || operator

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
)
like image 73
mersocarlin Avatar answered Apr 08 '26 09:04

mersocarlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!