![]() |
VOOZH | about |
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.
In 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.
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"
}
}
}
]