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)

boilermaker73
Inspiring
January 17, 2024

Below is an exact copy of the script I created and used in my attempt to rename a key of the JS object's property:

 

// sample script to rename key in JS object 'oVendors'
dsFld = getField("dataSrc");
oVendors = JSON.parse(dsFld.value);
for(var key in oVendors){
  if (oVendors['Citi Bank']){//if key exists continue to create a new key
    oVendors['Citi'] = oVendors['Citi Bank'];//assign old key and it's property named values to the new key
    delete oVendors['Citi Bank'];//delete the old key
  }
}
dsFld = JSON.stringify(oVendors);//convert oVendors data object back to a JSON string

 As you can see, the script employs single quotes. I should also add that the JS console displays no systax errors whatsoever in running the script.  Interestingly, I found an online site providing the exact same script (I had already created) for renaming a key of a JS object's property. Last  but not least, to clarify, the JS object is stored as a JSON string in a text field that resides hidden on my PDF form to explain the code to parse and eventually stringify the object for the purpose of working with JS object.