[Fixed] How can i filter data from array with more words in input?


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:

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.


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

Leave a Reply

(*) Required, Your email will not be published