Related Articles

Related Articles

Lodash _.map() Method
  • Last Updated : 06 Sep, 2020

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.

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

Syntax:

_.map( collection, iteratee )

Parameters: This method accepts two parameters as mentioned above and described below:

  • 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var 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);

chevron_right


Output:

[ 25, 324 ]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var 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);

chevron_right


Output:

[ 196, 784 ]

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var users = [
  { 'user': 'jonny' },
  { 'user': 'john' }
];
   
// Use of _.map() method
// The `_.property` iteratee shorthand
let mapped_array = _.map(users, 'user');
  
// Printing the output 
console.log(mapped_array);

chevron_right


Output:

[ 'jonny', 'john' ]

full-stack-img




My Personal Notes arrow_drop_up
Recommended Articles
Page :