Issue
i’m still learning angular and i want to make a search bar or filter to filter my fetched data from database, but i don’t know how. My data looks like this:
Data
I want to filter data with name and surname together. So when i type "John John" into my input field it will return me this array where the name and surname is John John. Filters i tried only filters by only name or only surname so when i typed John it returns me that array but when i typed John John it returns me nothing.
Thanks for the replies.
Solution
You have plenty of options depending on how "fuzzy" you want the search to be.
E.g. if you type "John John" shall user {name: "John", surname: "Doe"}
be in the results or must name
& surname
be matched?
A very basic approach would be to check if either the name or surname is part of the search-string. This could be done by filtering
the array via includes
:
(See full example on playground)
data.filter(user => searchParam.includes(user.name) || searchParam.includes(user.surname));