Lodash _.mapKeys() Method
Last Updated :
31 Oct, 2023
Lodash _.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:
- 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: In this example, we are adding the key and value by the use of the function that is passing into the lodash _.mapKeys() method and print the result in the console.
Javascript
const _ = require( "lodash" );
console.log(
_.mapKeys({ 'cpp' : 15, 'java' : 40, 'python' : 63 },
function (value, key) {
return key + value;
}
));
|
Output:
{'cpp15': 15, 'java40': 40, 'python63': 63}
Example 2: In this example, we are returning the age by the use of the function that is passing into the lodash _.mapKeys() method and print the result in the console.
Javascript
const _ = require( "lodash" );
let info = {
'GFG' : { 'user' : 'amit' , 'age' : 23 },
'codechef' : { 'user' : 'priya' , 'age' : 21 }
};
console.log(_.mapKeys(info,
function (o) { return o.age; })
);
|
Output:
{21: {'age': 21, 'user': "priya"}, 23: {'age': 23, 'user': "amit"}}
Share your thoughts in the comments
Please Login to comment...