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 18, 2024

In response to 'But why are you doing this with a "const" value?', I see your point. However, to clarify, I am not using the 'const' in my own script but inserted the 'const' in my initial post based on the sample script found online. As for double quotes, I checked the script on my computer and I am not using 'doubled up single quotes' in my script. In reality, I have tried both single and double quotes (not doubled up single quotes) in the script and, for whatever reason, the script still fails to work. On another note, what appears to suggest I was using 'doubled up quotes' in the script? However, I should also add that I have another script to change/rename property names of nested objects and it works as intended. All considered, I am completely dumbfounded as to why the script I created to change/rename the first level key/property names of my JS object fails to work as intended, hmmm? 


SORRY, PLEASE DISREGARD THIS REPLY.

OK. What I have come to learn is that while the script does in fact perform as intended, it does so transparently whereby it assigns a new property name (by reference) and deletes the old property name (by reference) transparently without changing/deleting the initial JS object property name stored in a JSON string inside a hidden text field that resides on my PDF form. What am I missing here given I falsely assumed the script I created would serve to rename/replace any one of the JS object properties stored inside the text field?