Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
27
rated 0 times [  29] [ 2]  / answers: 1 / hits: 25264  / 7 Years ago, thu, july 27, 2017, 12:00:00

My filter function here works great, however it only filters the first name (see code). So I was wondering what the best way to make it filter by surname and phone too (see image)!
Here's my filter at the moment:



const filteredUsers = this.state.dataToDisplay.filter(item => {
return (
/*Only firstname!*/item.name.toLowerCase().indexOf(this.state.search.toLowerCase()) >= 0
)
})


And here's the image:
enter



Thanks a lot! :D


More From » reactjs

 Answers
2

You are using filtering on the Javascript array level so just extend your criteria:



const filteredUsers = this.state.dataToDisplay.filter(item => {
const query = this.state.search.toLowerCase();

return (
item.name.toLowerCase().indexOf(query) >= 0 ||
item.surname.toLowerCase().indexOf(query) >= 0 ||
item.phone.toLowerCase().indexOf(query) >= 0
)
});

[#56948] Tuesday, July 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austynp

Total Points: 505
Total Questions: 118
Total Answers: 106

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
austynp questions
;