Home > Software engineering >  I want to be able to filter multiple conditions using filter in React.js
I want to be able to filter multiple conditions using filter in React.js

Time:08-08

I am making a system that can get json data with React and search for users by name. I want to be able to search by firstName or lastName I thought, but the following error message occurred during implementation.

'lastName' is not defined  no-undef

Therefore, it is not possible to search for multiple conditions.

  useEffect(() => {
    axios.get('https://api.hatchways.io/assessment/students')
    .then(result => {
      setPosts(result.data.students);
      setAllPosts(result.data.students);
    })
  }, []);

  const getSearchResult = (data) => {
    setSearchKeyword(data.search)
    const result = allPosts.filter((output, index) => {
      console.log(output)
      return output.firstName||lastName.includes(data.search);
    });
    console.log(result)
    setPosts(result);
  }

CodePudding user response:

Maybe return output.firstName || output.lastName.includes(data.search); ?

Or destructure it.

const { firstName, lastName } = output;

CodePudding user response:

maybe this is the code ?

output.firstName.includes(data.search) || output.lastName.includes(data.search);
  • Related