How to filter array of objects whose any properties contain a value with JavaScript?

To filter array of objects whose any properties contain a value with JavaScript, we call the filter method.

For instance, we write

const filterByValue = (array, string) => {
  return array.filter((o) =>
    Object.values(o).some((v) => v.toLowerCase().includes(string.toLowerCase()))
  );
};

const arrayOfObject = [
  { name: "Paul", country: "Canada" },
  { name: "Lea", country: "Italy" },
  { name: "John", country: "Italy" },
];

console.log(filterByValue(arrayOfObject, "lea"));

to define the filterByValue function.

In it, we call array.filter with a callback that calls Object.values with o to return an array of o‘s property values.

Then we call some with the array to check if v includes string if they’re both converted to lower case with toLowerCase with includes.

Next, we call filterByValue with arrayOfObject and the value we’re checking for.