Multiple conditions for result filter

I have an array of objects that I am filtering through based on a query. If the query matches a title inside the array of objects I return it.

  const arr = [{ title: 'Xbox one controller' }];

  const filterResults = arr.filter((item) =>
    item.title.toLowerCase().includes(req.query.q.toLowerCase())
  );

  res.send(filterResults);

These words if I just search on a word like “xbox”, however, if I search “xbox controller” it returns nothing.

I was thinking of splitting req.query like this: const reqy = req.query.q.split(' ') and using those as keywords although I don’t really know where to start. Another potential problem with my “solution” is that if a query looks like “Controller for Xbox One” will the result even show up?

My problem is making the filter function accept multiple “keywords” derived from the user query.

Answer

One possible solution is to check whether every word in your search term occurs in the title. You can call the split method on your searchTerm to get an array of words in the search term. Then call the every method to verify whether all of the words in the search term appear in the title. As before, you can still use the filter method to remove those entries whose titles do not contain all of the words in the search term.

const searchTerm = 'xbox controller';

const arr = [{ title: 'Xbox one controller' }];

const filterResults = arr.filter(({ title }) =>
  searchTerm
    .toLowerCase()
    .split(' ')
    .every(keyword => title.toLowerCase().includes(keyword))
);

Leave a Reply

Your email address will not be published. Required fields are marked *