How to Filter a Nested Array of Objects in JavaScript?

Estimated read time 1 min read

To filter a nested array of objects in JavaScript, you can use the Array.filter() method along with nested for loops to traverse the nested arrays. The Array.filter() method takes a callback function as an argument that defines the conditions for filtering the array.

Here’s an example of how to filter a nested array of objects:

let nestedArray = [
  {name: "John", age: 20, address: {city: "New York", country: "USA"}},
  {name: "Jane", age: 25, address: {city: "London", country: "UK"}},
  {name: "Jim", age: 30, address: {city: "Paris", country: "France"}}
];

let filteredArray = nestedArray.filter(function(obj) {
  return obj.address.country === "USA";
});

console.log(filteredArray);

In this example, the filteredArray will only contain the objects with address.country equal to “USA”.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply