Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Creating a Property Getter Function:
4.2.
JavaScript
4.3.
Using with Arrays of Objects:
4.4.
JavaScript
4.5.
Accessing Nested Object Properties:
4.6.
JavaScript
4.7.
Sorting Based on Object Properties:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
Can _.property() handle deep property paths?
5.2.
How does _.property() differ from directly accessing object properties?
5.3.
Is _.property() suitable for all types of objects?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.property() Method

Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Accessing properties of objects is a fundamental task in JavaScript programming, especially when working with collections or complex data structures. Lodash's _.property() method offers a succinct and convenient way to create a function that returns the value at a given path of an object. 

Lodash _.property() Method

This method is particularly useful in scenarios where you need to retrieve values from objects dynamically, such as in mapping, filtering, or sorting operations on collections.

Why This Function is Used

The _.property() function is used to create a function that returns the value of a specified property from an object. It's essential in situations where you need to extract property values from objects within higher-order functions like _.map(), _.filter(), or _.sortBy(), enabling more concise and readable code.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax:

 _.property(path)

Parameters:

path (Array|string): The path of the property to get.

Return Value: 

(Function) - Returns the new accessor function.

Examples 

Creating a Property Getter Function:

  • JavaScript

JavaScript

var _ = require('lodash');

var getName = _.property('name');

var user = { 'name': 'John', 'age': 30 };

console.log(getName(user));

Output:

 'John'


Demonstrates using _.property() to create a function that retrieves the 'name' property from an object.

Using with Arrays of Objects:

  • JavaScript

JavaScript

var users = [

 { 'user': 'barney', 'age': 36 },

 { 'user': 'fred', 'age': 40 }

];

var names = _.map(users, _.property('user'));

console.log(names);

Output:

['barney', 'fred']


Shows how to use a property getter function to extract values from an array of objects.

Accessing Nested Object Properties:

  • JavaScript

JavaScript

var getCountry = _.property('address.country');

var person = { 'name': 'John', 'address': { 'country': 'USA' } };

console.log(getCountry(person));

Output:

 'USA'


An example of using _.property() to access a nested property in an object.

Sorting Based on Object Properties:

  • JavaScript

JavaScript

var products = [

 { 'name': 'Apple', 'price': 1 },

 { 'name': 'Banana', 'price': 2 }

];

var sortedByPrice = _.sortBy(products, _.property('price'));

console.log(sortedByPrice);

Output: 

[{ 'name': 'Apple', 'price': 1 }, { 'name': 'Banana', 'price': 2 }]


Demonstrates sorting an array of objects based on a property value using _.property().

Frequently Asked Questions

Can _.property() handle deep property paths?

Yes, _.property() can handle deep property paths, allowing access to nested properties using dot notation or an array of property names.

How does _.property() differ from directly accessing object properties?

_.property() creates a function for property access, which is particularly useful in functional programming patterns and when working with higher-order functions.

Is _.property() suitable for all types of objects?

_.property() is versatile and suitable for most objects, but the property path must exist on the object for it to return a value; otherwise, it returns undefined.

Conclusion

Lodash's _.property() method is a handy tool for creating functions that retrieve specified properties from objects. It simplifies accessing object properties, especially in operations involving collections, enhancing code readability and functional programming practices.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.throttle() Method
Next article
Lodash _.isEqual() Method
Live masterclass