VOOZH about

URL: https://www.geeksforgeeks.org/javascript/finding-objects-using-lodash-with-nested-properties/

⇱ Finding Objects using Lodash with Nested Properties - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Finding Objects using Lodash with Nested Properties

Last Updated : 23 Jul, 2025

Finding objects by nested properties is a common task when dealing with complex data structures like deeply nested arrays or objects. Lodash, a popular JavaScript utility library, provides methods that simplify working with deeply nested data.

Prerequisites

Install Lodash:

Open your terminal and navigate to your project directory. Run the following command to install Lodash:

npm install lodash

Below are the approaches to finding objects by Nested Properties with Lodash:

Using Lodash find() method

In this approach we will use the find() method in Lodash this helps us to retrieve the first element in a collection that matches the given condition. Combining it with Lodash’s get allows us to access deeply nested properties without worrying about undefined errors.

Syntax:

_.find(collection, obj => _.get(obj, 'nested.property') === value);

Example: In below example we have nested object and we are finding the Object by Nested Properties using Lodash's find method.

Output:

{ id: 2, details: { name: 'Yuvraj Singh', age: 32 } }

Using Lodash filter() method

In this approach we will use Lodash filter() method. While find returns the first matching element, filter returns all elements that match a condition. We can use Lodash’s get to access nested properties.

Syntax:

_.filter(collection, obj => _.get(obj, 'nested.property') === value);

Example: In this example we have nested objct and we will find objects which have age greater than 25 using filter method of lodash.

Output:

[
{ id: 1, details: { name: 'Rohit Sharma', age: 28 } },
{ id: 2, details: { name: 'Yuvraj Singh', age: 32 } }
]
Comment