Non-tech founder’s guide to choosing the right software development partner Download Ebook
Home>Blog>Rename the key name in the javascript object

Rename the key name in the javascript object

How to rename the key name in the javascript object? That’s easy!

Lets create function to do that:


const renameKey = (object, key, newKey) => {

  const clonedObj = clone(object);

  const targetKey = clonedObj[key];



  delete clonedObj[key];

  clonedObj[newKey] = targetKey;

  return clonedObj;

};

Here is clone function:


const clone = (obj) => Object.assign({}, obj);

Example:


let contact = { 

    id: 1, 

    name: "contact name"

};



contact = renameKey(contact, 'id', 'value');

contact = renameKey(contact, 'name', 'label');



console.log(contact); // { value: 1, label: "contact name" };

Discover More Reads

Recent Projects

We take pride in creating applications that drive growth and evolution, from niche startups to international companies.

Do you have a tech idea?

Let’s talk!

By submitting this form, you agree with JetRockets’ Privacy Policy

If you prefer email, write to us at hello@jetrockets.com