Filter array of objects by values when not all objects have some keys

Liam McCormick

I am trying to filter an array of objects based on user input.

I wish filter based on the values of certain keys within the object. However, not all the objects have all keys inside them. This means when I check it throws an error.

How do I ignore if a particular object does not have one of the keys inside it and continues to output the matching objects?

Here is some sample data.

const data = [
{
    name: "John Miller",
    age: 33,
    location: "Chicago",
    address: '1 help street'
},

{
    name: "Jane Doe",
    age: 78,
    address: '1 help me please lane'
},

{
    name: "Jamie Stevens",
    location: "San Diego",
    age: 32
}
]

The second object does not have 'location' as a key and the third object does not have 'address' as a key.

const handleSearch = (query) => {
    const keys = ['name', 'location', 'address']
    const filter =  data.filter((row) => (
      keys.some((key) => (row[key].toLowerCase().includes(query))
    )))
    setFilteredData(filter)
  }

Thank you,

Amila Senadheera

Use the optional chaining operator(?.) and you also need to lower case the query:

const data = [
  {
    name: "John Miller",
    age: 33,
    location: "Chicago",
    address: "1 help street",
  },

  {
    name: "Jane Doe",
    age: 78,
    address: "1 help me please lane",
  },

  {
    name: "Jamie Stevens",
    location: "San Diego",
    age: 32,
  },
];

const handleSearch = (query) => {
  const keys = ["name", "location", "address"];
  const filtered = data.filter((row) =>
    keys.some((key) => { 
      return row[key]?.toLowerCase().includes(query.toLowerCase())})
  );
  console.log(filtered);
};

handleSearch("Chicago")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get all keys in array of objects whose values are arrays

Extracting all values from a Array of Objects based on keys value

Filter array of objects by sub array of values

Filter two values in array of objects

Filter array of objects by multiple values

Filter array of objects by another array with duplicate keys

How to use Array.protoype.map() on array of objects to filter out some specific keys based on it's values?

Print out all objects with keys and values

How to filter an array of objects, based on some fields?

JS Array of Objects filter out values in an Array

How to check if all objects in an array contains same keys and values?

Finding all the keys in the array of objects

Array of objects with some objects containing another array, need to filter it by values within the object containing an array

Search for all values in array of objects

Filter all values in array of objects

Sum array of objects values by multiple keys in array

Search in array of objects in JavaScript in all keys values

I have an Array of objects, in all objects I have delete some data

how to remove objects that have all keys with null values in dataweave?

filter array of objects based on separate object values by keys using reactjs

How to use .some() for all values in an array with different objects

Filter an array of objects with keys

How to filter array of objects by object keys?

Filter array of objects in which not all keys are present - JS

initialization of an array of objects in a for loop, this cause all the objects have the same values, help please

how to output an objects keys when some key/value pairs have different keys but same values

Is there any way to filter objects from array of objects that should include all selected values in javascript

How to filter an array of objects when object key contains specific values

How do I filter objects based on keys which have value as an array in React?