Syntax, Parameter and Return Value
Syntax:
_.at(object, [paths])
Parameters:
-
object (Object): The object to query.
- [paths] (Array|string): The paths of the properties to get.
Return Value:
(Array) - Returns the array of picked values.
Examples
Picking Multiple Properties:
JavaScript
var _ = require('lodash');
var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
console.log(_.at(object, ['a[0].b.c', 'a[1]']));

You can also try this code with Online Javascript Compiler
Run Code
Output:
[3, 4]
Demonstrates retrieving values at specified paths from a nested object.
Extracting Data from Complex Structures:
JavaScript
var userData = {
name: 'John Doe',
age: 30,
contact: { email: 'john@example.com', phone: '1234567890' }
};
var userDetails = _.at(userData, ['name', 'contact.email']);
console.log(userDetails);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['John Doe', 'john@example.com']
Shows how to extract specific pieces of data from a complex object.
Using with Array of Objects:
JavaScript
var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 }
];
var ages = _.at(users, ['0.age', '1.age']);
console.log(ages);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[48, 36]
An example of using _.at() to extract properties from an array of objects.
Combining with Other Lodash Methods:
JavaScript
var complexData = {
users: [
{ 'user': 'fred', 'active': true },
{ 'user': 'barney', 'active': false }
],
counts: [1, 2, 3]
};
var pickedData = _.map(complexData.users, user => _.at(user, ['user', 'active']));
console.log(pickedData);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[['fred', true], ['barney', false]]
Demonstrates combining _.at() with _.map() for data transformation.
Frequently Asked Questions
How does _.at() differ from direct property access?
_.at() simplifies accessing multiple properties, especially from nested objects, and returns the values in an array. It's more concise compared to manually accessing each property, particularly in nested or complex objects.
Can _.at() handle invalid or undefined paths?
If a path does not exist in the object, _.at() will return undefined for that path.
Is _.at() efficient for extracting a large number of properties?
_.at() is designed for convenience and readability. While it's efficient for most use cases, the performance should be considered when dealing with a very large number of properties or extremely deep objects.
Conclusion
Lodash's _.at() method provides a straightforward and efficient way to extract values from multiple paths within an object. It is particularly useful for simplifying data access in nested structures and enhances the readability and maintainability of code involving complex data manipulation.
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.