Primitive full-text search on some properties of an object I am not a JS/TS Guru, so i wanted to ask if one could write this better.

I needed a very primitive Full-Text-Search of some properties of an Object. What do you think?

export const search = &lt;T, K extends keyof T&gt;(term: string, objects: T[], keys: K[]): T[] =&gt; {
  const foundObjects = objects.filter(x =&gt; {
    for (const key of keys) {
      if (x.hasOwnProperty(key) === false) {
        continue;
      }

      const val = x[key];

      if (typeof val === 'string' &amp;&amp; val.toLowerCase().includes(term)) {
        return true;
      } else {
        continue;
      }
    }
    return false;
  });
  return foundObjects;
};
You have a lot of code that is not needed.


The two continue are not needed.
Rather than assign the variable foundObjects the filtered result just return the result directly.
The variable val that holds the property can be side stepped.
Then the hasOwnProperty can be combined with the if statement
Rather than use the for loop you can also use Array.some to find if any item matches the search


Thus you get

export const search = &lt;T, K extends keyof T&gt;(term: string, objects: T[], keys: K[]): T[] =&gt; {
    return objects.filter(x =&gt; 
        keys.some(key =&gt; 
            x.hasOwnProperty(key) &amp;&amp; 
            typeof x[key] === 'string' &amp;&amp; 
            x[key].toLowerCase().includes(term)
        )
    );
};