How to filter nested objects in JavaScript? Last Updated : 24 Jun, 2024 Comments Improve Suggest changes Like Article Like Report The filter() method creates a new array with all elements that pass the test implemented by the provided function. Below are the approaches used to filter nested objects in JavaScript: Table of Content Using filter() methodUsing some() method Method 1: Using filter() method Example: This approach uses the filter() method to filter the nested object in JavaScript. HTML <!DOCTYPE html> <html> <body> <h2>Output</h2> <p id="Output"></p> <script> var nestedObject = [ { itemId: 1, itemDetails: { name: "C", caregory: "Programming Language", price: 500, }, itemCategory: "Basic", }, { itemId: 2, itemDetails: { name: "C++", caregory: "Programming Language", price: 700, }, itemCategory: "Beginner", }, { itemId: 1, itemDetails: { name: "Java Script", caregory: "Web Development", price: 1500, }, itemCategory: "Advanced", }] let itemNames = nestedObject.filter( eachObj => eachObj.itemDetails.price === 1500); document.getElementById("Output").innerHTML = JSON.stringify(itemNames); </script> </body> </html> Output: OutputMethod 2: Using some() method Example: This approach uses some() method to filter the nested objects. The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value. HTML <!DOCTYPE html> <html> <body> <h2>Output</h2> <p id="Output"></p> <script> const fruitData = [ { name: "Apples", details: [ { fruitId: '1', gradingDetails: [{ grade: 'A' }] }, { fruitId: '2', gradingDetails: [{ grade: 'B' }] } ] }, { name: "Oranges", details: [ { fruitId: '3', gradingDetails: [{ grade: 'B' }] }, { fruitId: '4', gradingDetails: [{ grade: 'D' }] } ] }, ]; let output = fruitData.filter(eachVal => { let opt = eachVal.details.some(( { gradingDetails }) => gradingDetails .some(({ grade }) => grade === 'A')); return opt; }) console.log(output); document.getElementById("Output").innerHTML = JSON.stringify(output); </script> </body> </html> Output: Output Comment More infoAdvertise with us Next Article How to filter nested objects in JavaScript? P prasannareddyisireddy Follow Improve Article Tags : JavaScript Web Technologies HTML HTML-Misc JavaScript-Misc HTML-Questions +2 More Similar Reads How to Create a Nested Object in JavaScript ? JavaScript allows us to create objects having the properties of the other objects this process is called as nesting of objects. Nesting helps in handling complex data in a much more structured and organized manner by creating a hierarchical structure. These are the different methods to create nested 4 min read How to Filter Nested Object in Lodash? Lodash is a popular JavaScript utility library that simplifies common tasks like manipulating arrays, objects, strings, and more. In this tutorial, we will explore how to filter nested objects using Lodash's _.filter() and _.get() methods. This technique is useful when we need to filter data, such a 2 min read How to Compare Objects in JavaScript? Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to co 3 min read How to implement a filter() for Objects in JavaScript ? In this article, we will learn how to implement a filter() for Objects in JavaScript. In JavaScript, the filter() method is a built-in function for the array, not for the object. The filter() method outputs all the elements of an array that pass a specific test or satisfies a specific function. The 3 min read How to Remove Null Objects from Nested Arrays in JavaScript ? Javascript arrays are a very flexible container that can hold both elements or values. Nevertheless, with nested arrays, it is crucial for data management and cleaning. Replacing null objects is a frequent demand.Below are the methods to remove null objects from nested arrays in JavaScript:Table of 9 min read Like