9951 explained code solutions for 126 technologies


javascript-lodashHow can I rename a key in an object using Lodash in JavaScript?


Using Lodash in JavaScript, you can rename a key in an object by using the _.set method. This method allows you to set a value on an object given a path. Here is an example:

let myObject = { name: 'Bob' };

_.set(myObject, 'firstName', myObject.name);
delete myObject.name;

console.log(myObject);
// Output: { firstName: 'Bob' }

The _.set method takes three parameters:

  1. The object you want to update.
  2. The path of the value you want to set.
  3. The value you want to set.

In this example, the first parameter is myObject, the second parameter is 'firstName', and the third parameter is myObject.name. The _.set method will set myObject.firstName to myObject.name and then delete myObject.name.

Helpful links

Edit this code on GitHub