How to Filter Nested Object in Lodash? Last Updated : 02 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 as selecting users based on specific criteria.ApproachIn this approach, we have defined the sample array of nested objects and displayed it in its raw form using console.log(). We then create a function, filterData(), which uses Lodash's _.filter() method to process this data by applying a filter condition, in this case, to select objects where the details.age is greater than 24. After filtering, the function prints the resulting subset of data. Example: This example shows how to filter nested Objects in Lodash. JavaScript // Import lodash const _ = require('lodash'); // Sample nested object data const data = [ { id: 1, details: { age: 25, name: 'Alice', address: { city: 'New York' } } }, { id: 2, details: { age: 30, name: 'Bob', address: { city: 'Los Angeles' } } }, { id: 3, details: { age: 22, name: 'Charlie', address: { city: 'Chicago' } } }, ]; // Function to filter the data function filterData() { // Use Lodash to filter the data const filteredData = _.filter(data, (item) => _.get(item, 'details.age') > 24); // Output the filtered data console.log("After Filtered Data:"); console.log(JSON.stringify(filteredData, null, 2)); } // Run the filterData function filterData(); Output:After Filtered Data:[ { "id": 1, "details": { "age": 25, "name": "Alice", "address": { "city": "New York" } } }, { "id": 2, "details": { "age": 30, "name": "Bob", "address": { "city": "Los Angeles" } } }] Comment More infoAdvertise with us Next Article How to Filter Nested Object in Lodash? S skaftafh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Lodash Similar Reads How to filter nested objects in JavaScript? 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 us 2 min read How to Filter Object by Keys or Values in Lodash? Filtering objects is a common requirement when we want to extract certain data from larger datasets. Lodash provides convenient methods to do this, which helps us in avoid writing complex loops and conditional statements. Below are different approaches to filter objects by keys or values in lodash:T 3 min read How to Filter Keys of an Object with Lodash ? Filtering Keys of an Object is used for selectively including or excluding keys based on criteria, facilitating data manipulation and customization. Below are the approaches to filter keys of an object with Lodash: Table of Content Using pick functionUsing omit functionUsing pickBy functionInstallin 2 min read How to Filter Key of an Object using Lodash? Filtering keys of an object involves selecting specific keys and creating a new object that contains only those keys. Using Lodash, this process allows you to include or exclude properties based on specific criteria, simplifying object manipulation. Below are the approaches to filter keys of an obje 2 min read How to Convert Object to Array in Lodash ? Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format. Below are the different approaches to converting objects to arrays in Lodash: Table of Content Using toArray function Using values functionRun the below command before running the below 2 min read Like