The Wayback Machine - https://web.archive.org/web/20240123045737/https://www.geeksforgeeks.org/lodash-_-map-method/
Open In App
Related Articles

Lodash _.map() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Lodash _.map() method creates an array of values by running each element in the collection through the ‘iteratee’. There are many methods that are guarded to work as iteratees for methods like _.every(), _.filter(), _.map(), _.mapValues(), _.reject(), and _.some() methods.

Syntax:

_.map(collection, iteratee)

Parameters:

  • collection: This parameter holds the collection to iterate over.
  • iteratee: This parameter holds the function invoked per iteration.

Return Value:

This method returns the new mapped array.

Example 1: In this example, we have used the _.map() method on an array and do iteration while using function square.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let array = _.map([5, 18]);
 
// Use of _.map() method
let mapped_array =
    _.map(array, function square(n) {
    return n * n;
})
 
// Printing the output
console.log(mapped_array);


Output:

[ 25, 324 ]

Example 2: In this example, we have used the _.map() method on an object and doing iteration while using function square.

javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array
let array = _.map({ 'x': 14, 'y': 28 });
 
// Use of _.map() method
let mapped_array =
    _.map(array, function square(n) {
    return n * n;
})
 
// Printing the output
console.log(mapped_array);


Output:

[ 196, 784 ]
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 18 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials