Thursday, November 20, 2025
HomeLanguagesJavascriptLodash _.mapValues() Method

Lodash _.mapValues() Method

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.

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 = {
'Geeksforneveropen': {
    '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:

 

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

 

Example 2:  

 

Javascript




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


Output:

{Geeksforneveropen: "gfg@123", W3school: "w@123"}
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6775 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6903 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS