Lodash _.renameKeys() method takes an object and a mapping object and returns a new object where the keys of the given object have been renamed as the corresponding value in the keyMap.
Syntax:
_.renameKeys(obj, mapObj);
Parameters:
- obj: Given object to create a new object.
- mapObj: Given map object to create a new object.
Return Value:
This method returns a generated object.
Note:
This will not work in normal JavaScript because it requires the lodash.js contrib library to be installed. Lodash.js contrib library can be installed using the following command:
npm install lodash-contrib
Example 1: This example shows the use of the Lodash _.renameKeys() method.
javascript
// Defining underscore lodash variable let _ = require( 'lodash-contrib' );
// Declare and object and rename its key let obj = _.renameKeys({ 1: "Geeks" ,
2: "Computer_Science_Portal"
}, { 1: "g" , 2: "c" });
console.log( "Generated Object: " , obj);
|
Output:
Generated Object: Object {c: "Computer_Science_Portal", g: "Geeks"}
Example 2: This example shows the use of the Lodash _.renameKeys() method.
javascript
// Defining underscore lodash variable let _ = require( 'lodash-contrib' );
// Declare and object and rename its key let obj = _.renameKeys( {
1: "Geeks" , 2: "Computer_Science_Portal" ,
3: "Geeks"
}, { 1: "g" , 2: "c" , 3: "g" });
console.log( "Generated Object: " , obj);
|
Output:
Generated Object: Object {c: "Computer_Science_Portal", g: "Geeks"}