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

Lodash _.mapKeys() 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 _.mapKeys() method is used to create an object with the same values as the object and the keys created by running each of the object’s own enumerable string keys.

Syntax:

_.mapKeys( object, iteratee )

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

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

Return Value: This method returns the new mapped object.

Example 1:

Javascript




// Requiring the lodash library  
const _ = require("lodash");
  
// Using the _.mapKeys() method 
console.log(
  _.mapKeys({ 'cpp': 15, 'java': 40, 'python': 63 },
      function(value, key) {
          return  key + value ;
  }
));


Output:

{'cpp15': 15, 'java40': 40, 'python63': 63}

Example 2:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// The source object
var info = {
  'GFG': { 'user': 'amit', 'age': 23 },
  'codechef': { 'user': 'priya', 'age': 21 }
};
  
// Using the _.mapKeys() method 
console.log(_.mapKeys(info,
  function(o) { return o.age; })
);


Output:

{21: {'age': 21, 'user': "priya"}, 23: {'age': 23, 'user': "amit"}}
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6778 POSTS0 COMMENTS
Nicole Veronica
11927 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7163 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS