'Search doesn't work when it has spaces or different type case characters

I'm working on a search that partly works but it has some bugs, the first one is that doesn't search the characters that differen in case, for example: StackOverflow is not 'Stackoverlow`, but I want to match it even if they differ in casing.

The other problem is that doesn't search the white spaces, as long as is without spaces it works but when I try to search for example: Stack Overflow, then it doesn't find the Stack Overflow even if it exits.

This search is that made to search all values inside an object. Waiting for better code, let's see!

Here is the code:

const findIn = (arr, query) => {
  return arr.filter((obj) =>
    Object.keys(obj).some((key) => {
      if (typeof obj[key] === 'string') return obj[key].includes(query);
    })
  );
};

As requested here is how the object looks: screenshot



Solution 1:[1]

You can solve your problem by format your string to lowercase and remove spaces and also in your obj.

  const findIn = (arr, query) => {
     let queryFormatted = (query.toLowerCase()).trim();
     return arr.filter((obj) =>
       Object.keys(obj).some((key) => {
        if (typeof obj[key] === 'string'){
          return ((obj[key].toLowerCase()).trim()).includes(queryFormatted);
          }
     })
  );
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1