How to filter a complex JSON Object using JavaScript?

Estimated read time 1 min read

To filter a complex JSON object using JavaScript, you can use the Array.filter() method, which creates a new array with all elements that pass the test implemented by the provided function. Here’s an example:

const originalArray = [
  { name: "John", age: 25, city: "New York" },
  { name: "Jane", age: 32, city: "London" },
  { name: "Jim", age: 41, city: "Paris" },
  { name: "Joan", age: 29, city: "Berlin" }
];

const filteredArray = originalArray.filter(item => item.age > 30);

console.log(filteredArray);

In this example, originalArray is an array of JSON objects. The filter() method creates a new array filteredArray containing all objects whose age property is greater than 30. The function passed to filter() tests each item in the original array and returns true or false depending on whether it should be included in the new array.

You can adjust the logic in the function passed to filter() to suit your specific needs for filtering the JSON object.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply