简体   繁体   中英

Filtering Data By Full Name React

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"

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());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM