Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To apply filtering to an object that is nested in JavaScript, you can use a combination of array methods such as filter(), map(), and reduce(). Here's an example of how to filter an array of objects based on a nested property:

const data = [
  { name: 'John', age: 30, address: { city: 'New York', state: 'NY' } },
  { name: 'Jane', age: 25, address: { city: 'Los Angeles', state: 'CA' } },
  { name: 'Bob', age: 40, address: { city: 'Chicago', state: 'IL' } },
];

const filteredData = data.filter((item) => item.address.city === 'New York');

console.log(filteredData);
// Output: [{ name: 'John', age: 30, address: { city: 'New York', state: 'NY' } }]

In this example, we use the filter() method to iterate through the array and return only the objects where the value of the city property in the nested address object is equal to 'New York'.