Filtering Data By Full Name React

Viewed 430

Hi I am Trying to filter my Data By Full Name, my API takes in a first and last name and I am returning it as:

const filteredStudents = students.filter( student => {
    return (student.firstName.toLowerCase().includes(search.toLowerCase()) + student.lastName.toLowerCase().includes(search.toLowerCase()));
})

which only shows a result when typing a first or last name is there a way to handle a condition for when you type in the whole name?

For Example, "Bill Gates"

1 Answers

You can concatenate the names first, then check if the search term is contained.

const filteredStudents = students.filter((student) => {
  return `${student.firstName} ${student.lastName}`
    .toLowerCase()
    .includes(search.toLowerCase());
});
Related