Skip to main content
boilermaker73
Inspiring
January 17, 2024
Question

Script to rename a key of a JS object's property

  • January 17, 2024
  • 1 reply
  • 4604 views

Can someone explain why the following simple/basic script to rename the key of an object's property appears to work for the JS object displayed below

 

const obj = { firstName: 'Sling', lastName: 'Academy' };

obj.name = obj.firstName; // create a new property with the same value

delete obj.firstName; //delete the old property

 

but fails to work for a JS object of the type provided below:

 

const obj = {firstName:{address:'' '',city:'' '', state:'' '', zip:'' '', phone:'' ''},
secondName:{address:'' '',city:'' '', state:'' '', zip:'' '', phone:'' ''},
thirdName: {address:'' '',city:'' '', state:'' '', zip:'' '', phone:'' ''},
.
.
.
}}

All considered, I am curious to know what changes may be made to the script initially provided to rename a key or keys for the JS object shown above to rename firstName, secondName, thirdName, …..

Thank you ahead of time. 

This topic has been closed for replies.

1 reply

Nesa Nurani
Community Expert
Community Expert
January 17, 2024

It's your qoutes, try like this:

const obj = {
  firstName: { address: '', city: '', state: '', zip: '', phone: '' },
  secondName: { address: '', city: '', state: '', zip: '', phone: '' },
  thirdName: { address: '', city: '', state: '', zip: '', phone: '' }
};

obj.name = obj.firstName;
delete obj.firstName;
console.log(obj);
boilermaker73
Inspiring
January 17, 2024

Sorry Nesa, but double quotes do not appear to have anything to do with the script not working as I have already tried both not to mention, according to consensus, both single and double quotes are considered acceptable JS syntax in this particular instance (refer to link below courtesy of W3Schools and scroll down to strings) and as such don't make a difference. I probably should have also stated that I have a similar script I created for changing a nested object's property name and it works like a charm that makes the 'rename key' issue all the more surprising.  Thanks for the response anyway.

JavaScript Syntax (w3schools.com)

Inspiring
January 18, 2024

Issue resolved.  For anyone interested, refer to sample script below that changes/renames the top most level JS object property (nested object) stored inside a text field in JSON format which is what I wanted to accomplish from the getgo. In my particular case, I finally realized I needed to use bracket as opposed to dot notation in accessing the JS object property, an inadvertent oversight.

 

// sample script to change/rename a JS object/property name by assigning the old property name to a new property name and deleting the old property name
dsFld =getField("dataSrc");// get stored text fld
oVendors = JSON.parse(dsFld.value);// parse JSON string
if (oVendors['Amazon Prime']){//check if prop exists in oVendors
oVendors['Amazon'] = oVendors['Amazon Prime'];//create new property & assign value of old to new
delete oVendors['Amazon Prime'];//delete old property
}
dsFld.value=JSON.stringify(oVendors);


You already got correct answer to your question, your quotes are wrong, this is what you get when you paste your script into editor