DEV Community

Discussion on: Simple search form in REACT using hooks 🔎.

Collapse
 
clarity89 profile image
Alex K. • Edited

I think it'd be const results = !searchTerm && !searchTerm2 instead of || otherwise the filtering will be applied only of both search terms are present.

Thread Thread
 
aa_ziz profile image
Ahmed Aziz
  const results =
    searchTerm || searchTerm2
      ? !searchTerm2
        ? people.filter(person =>
            person.name.toLowerCase().includes(searchTerm.toLocaleLowerCase())
          )
        : people.filter(
            person =>
              person.address.toLowerCase() ===
              searchTerm2.toLocaleLowerCase()
          )
      : people;

Enter fullscreen mode Exit fullscreen mode

i came up with this solution and it works now but only if one of the conditions is true. I want to make the method filter the two conditions in the same time. So like i want to know all the people that their name is Alex and lives in New york

Thread Thread
 
clarity89 profile image
Alex K.

For your specific case you can do like this:

const results = searchTerm && searchTerm2 ?
  people.filter(person =>
    person.name.toLowerCase().includes(searchTerm.toLocaleLowerCase()) &&
    person.address.toLowerCase().includes(searchTerm2.toLocaleLowerCase())
  ) : people

However, note that the filter won't be applied unless both search terms are present.

Thread Thread
 
aa_ziz profile image
Ahmed Aziz

Yah but that is exactly what i don't want. I want that the user can choose one of the filters or both of them in the sametime

Thread Thread
 
luxer066 profile image
Lucas Canavesio

Hello! In the end you found the solution?