Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lodash _.mapValues() Method

  • Last Updated : 22 Mar, 2021

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

The _.mapValues() method is used to create a new mapped object with the same keys of the given object and the values are generated using the given iteratee function.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax:

_.mapValues( object, iteratee )

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



  • object: This parameter holds the object to iterate over.
  • iteratee: This parameter holds the function that is invoked per iteration on the object. It is an optional value.

Return Value: This method returns the new mapped object.

Example 1:

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
 
var users = {
'Geeksforgeeks': {
    'username': 'gfg_id',
    'password': 'gfg@123'
  },
  'W3school': {
    'username': 'w3school_id',
    'password': 'w@123'
  }
};
 
// Using the _.mapValues() method
console.log(
  _.mapValues(users, function(o) {
    return o.password;
  })
);

 

 

Output:

 

{Geeksforgeeks: "gfg@123", W3school: "w@123"}

 

Example 2:  

 

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
 
var users = {
  'Geeksforgeeks': {
    'username': 'gfg_id',
    'password': 'gfg@123'
  },
  'W3school': {
    'username': 'w3school_id',
    'password': 'w@123'
  }
};
 
// Using the _.mapValues() method
console.log(_.mapValues(users, 'password'));

Output:

{Geeksforgeeks: "gfg@123", W3school: "w@123"}



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!