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.
Picking Multiple Properties:
4.2.
JavaScript
4.3.
Extracting Data from Complex Structures:
4.4.
JavaScript
4.5.
Using with Array of Objects:
4.6.
JavaScript
4.7.
Combining with Other Lodash Methods:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.at() differ from direct property access?
5.2.
Can _.at() handle invalid or undefined paths?
5.3.
Is _.at() efficient for extracting a large number of properties?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.at() Method

Author Riya Singh
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Accessing values at specific paths within an object or an array is a common task in programming, particularly when dealing with complex data structures. Lodash enhances this capability with its _.at() method. 

Lodash _.at() Method

This method allows for the extraction of values from an object at specified paths, which is particularly useful in scenarios involving nested data structures or when you need to retrieve multiple properties from an object in a single operation.

Why This Function is Used

The _.at() function is used to create an array of values corresponding to paths of properties in an object. This method is essential when you need to pick multiple properties from an object without manually accessing each one, especially useful in Data manipulation, mapping, and transformation tasks where properties from deep within an object need to be extracted efficiently.

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: 

_.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

JavaScript

var _ = require('lodash');

var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };

console.log(_.at(object, ['a[0].b.c', 'a[1]']));

 Output: 

[3, 4]


Demonstrates retrieving values at specified paths from a nested object.

Extracting Data from Complex Structures:

  • JavaScript

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);

Output:

 ['John Doe', 'john@example.com']


Shows how to extract specific pieces of data from a complex object.

Using with Array of Objects:

  • JavaScript

JavaScript

var users = [

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

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

];

var ages = _.at(users, ['0.age', '1.age']);

console.log(ages);

Output

[48, 36]


An example of using _.at() to extract properties from an array of objects.

Combining with Other Lodash Methods:

  • JavaScript

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);

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 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 _.functionsIn() Method
Next article
Lodash _.meanBy() Method
Live masterclass