2

Pls see below my code,trying to search by comparing the input value with multiple object values.

<input type="search" placeholder="Search by Topic, Level or Subject..."
  onChange={(e) => {
    const searchGames = search.filter(item => item.Topic.toLowerCase().includes(e.target.value.toLowerCase()) ? item : null);
    setGame(searchGames);
    const searchGames2 = search.filter(item => item.Level.toLowerCase().includes(e.target.value.toLowerCase()) ? item : null);
    setGame(searchGames2);
    const searchGames3 = search.filter(item => item.Subject.toLowerCase().includes(e.target.value.toLowerCase()) ? item : null);
    setGame(searchGames3);
  }}
/>
1
  • 1
    Do You mean to say you need to search Topic, Level and Subject and combined its results? Commented Jul 8, 2022 at 16:48

1 Answer 1

2

Seems like a more of programming question than react one.

As you may have noticed your example code lacked "or".

Try just checking if any of the conditions are true.

<input type="search" placeholder="Search by Topic, Level or Subject..."
  onChange={(e) => {
    const filteredGames = search.filter(item => {
      const topicIncludes = item.Topic.toLowerCase().includes(e.target.value.toLowerCase());
      const levelIncludes = item.Level.toLowerCase().includes(e.target.value.toLowerCase());
      const subjectIncludes = item.Subject.toLowerCase().includes(e.target.value.toLowerCase());
      return topicIncludes || levelIncludes || subjectIncludes;
    })
    setGame(filteredGames)
  }}
/>

Also, setting a state multiple times in a row is almost never a good idea.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.