I am trying to filter an array of users, which has a property called name, that when trying to filter by the string the order is fine.
const users = [
{ name: 'bob simmons', age: 25 },
{ name: 'samantha simpsoms', age: 19 },
{ name: 'karl bone romero', age: 33 },
{ name: 'bob bone cruz', age: 33 }
]
const getUsers = (prop) => users.filter(({name}) => name.match(prop))
getUsers('bob')
// [{ name: 'bob simmons', age: 25 }, { name: 'bob bone cruz', age: 33 }]
getUsers('bob bone')
// [{ name: 'bob bone cruz', age: 33 }]
Well perfect, it is working, the problem I currently have is when I try to bring the name as it is not in the name property. Now what I'm trying to do is look for parts of the string but not in the order that is declared for example ...
search: bob cruz result {name: 'bob bone cruz', age: 33}
search: karl romero result { name: 'karl bone romero', age: 33 }
try to split the search parameter and then inside the filter ask with some if they exist include the text inside the name, the problem with this is that it returns all the search matches.
const getUsers = (prop) => users.filter(({ name }) => {
return prop.split(' ').some(e => name.includes(e))
})
getUsers('bob cruz')
// [{ name: 'bob simmons', age: 25 }, { name: 'bone bob cruz', age: 33 }]
I just need you to return [{ name: 'bone bob cruz', age: 33 }]