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
var _ = require('lodash');
var getName = _.property('name');
var user = { 'name': 'John', 'age': 30 };
console.log(getName(user));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'John'
Demonstrates using _.property() to create a function that retrieves the 'name' property from an object.
Using with Arrays of Objects:
JavaScript
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
var names = _.map(users, _.property('user'));
console.log(names);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['barney', 'fred']
Shows how to use a property getter function to extract values from an array of objects.
Accessing Nested Object Properties:
JavaScript
var getCountry = _.property('address.country');
var person = { 'name': 'John', 'address': { 'country': 'USA' } };
console.log(getCountry(person));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'USA'
An example of using _.property() to access a nested property in an object.
Sorting Based on Object Properties:
JavaScript
var products = [
{ 'name': 'Apple', 'price': 1 },
{ 'name': 'Banana', 'price': 2 }
];
var sortedByPrice = _.sortBy(products, _.property('price'));
console.log(sortedByPrice);

You can also try this code with Online Javascript Compiler
Run Code
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.