Skip to content
Advertisement

how to filter array in reactJS with 2 condistions

I try to filter the array with two conditions but without success, it only refers to the second condition, each condition individually works great.

The original task : “When entering a text in the “search” text box, the users list will presents anly users that
their name or email contains that text

const filteredUsers = this.state.users.filter((filuser) => {
  return (
    filuser.name.toLowerCase().includes(this.state.searchfield.toLowerCase()) ||
    filuser.email.toLowerCase().includes(this.state.searchfield.toLowerCase())
  );
});

Advertisement

Answer

const filteredUsers = this.state.users.filter((filuser) => {
  const {searchfield} = this.state
  return (
    `${filuser.name} ${filuser.email}`.toLowerCase().indexOf(searchfield)>-1
});
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement