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.
Mapping Array Elements:
4.2.
JavaScript
4.3.
Extracting Properties from Objects:
4.4.
JavaScript
4.5.
Mapping Over an Object:
4.6.
JavaScript
4.7.
Mapping Over a String:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.map() differ from native JavaScript Array.map()?
5.2.
Can _.map() modify the original collection?
5.3.
Is _.map() efficient for large datasets?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.map() Method

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

Introduction

Transforming collections is a frequent necessity in JavaScript, and the Lodash library offers a powerful tool for this: the _.map() method. It creates a new array of values by running each element in a collection through an iteratee function. 

Lodash _.map() Method

This article will explore the _.map() method, discussing its syntax, diverse applications, and benefits, all illustrated through examples and FAQs.

Why This Function is Used

The _.map() function is used for iterating over elements in a collection (like an array, object, or string) and transforming them based on a specified function. This is crucial in scenarios such as data transformation, applying computations, or extracting specific properties from objects. It enhances code readability and efficiency, providing a functional approach to iterating and transforming data.

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: 

_.map(collection, [iteratee=_.identity])

Parameters:

  • collection (Array|Object|string): The collection to iterate over.
     
  • [iteratee=_.identity] (Function): The function invoked per iteration.

Return Value:

 (Array) - Returns the new mapped array.

Examples 

Mapping Array Elements:

  • JavaScript

JavaScript

var _ = require('lodash');

var numbers = [1, 2, 3];

var doubled = _.map(numbers, n => n * 2);

console.log(doubled);

Output: 

[2, 4, 6]


Demonstrates multiplying each number in an array.

Extracting Properties from Objects:

  • JavaScript

JavaScript

var users = [{ 'user': 'barney', 'age': 36 },

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

var ages = _.map(users, 'age');

console.log(ages);

Output:

[36, 40]


Shows how to extract a specific property from each object in an array.

Mapping Over an Object:

  • JavaScript

JavaScript

var object = { 'a': 1, 'b': 2, 'c': 3 };

var valuesSquared = _.map(object, n => n * n);

console.log(valuesSquared);

Output: 

[1, 4, 9]


An example of applying a function to each value in an object.

Mapping Over a String:

  • JavaScript

JavaScript

var string = "hello";

var asciiValues = _.map(string, char => char.charCodeAt(0));

console.log(asciiValues);

Output: 

[104, 101, 108, 108, 111]


Demonstrates mapping each character in a string to its ASCII value.

Frequently Asked Questions 

How does _.map() differ from native JavaScript Array.map()?

Lodash's _.map() works with a broader range of collections, including objects and strings, whereas the native Array.map() is limited to arrays.

Can _.map() modify the original collection?

No, _.map() returns a new array and does not modify the original collection, adhering to the principles of functional programming.

Is _.map() efficient for large datasets?

_.map() is generally efficient for large datasets, but performance depends on the complexity of the iteratee function and the size of the dataset.

Conclusion

Lodash's _.map() method is a versatile and essential tool for transforming collections in JavaScript. It offers a concise and readable way to apply a function to each element of a collection, producing a new array of transformed elements.

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