• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
10

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

Engaged ,
Jan 16, 2024 Jan 16, 2024

Copy link to clipboard

Copied

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. 

TOPICS
How to , JavaScript , PDF , PDF forms

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 16, 2024 Jan 16, 2024

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

Nesa is correct.  The quotes in the code block are not double quotes. They are doubled up single quotes. If you fix the code it all works. 

 

But why are you doing this with a "const" value?  It brobably shouldn't work at all. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

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? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

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?  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 17, 2024 Jan 17, 2024

Copy link to clipboard

Copied

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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

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

Izrezak.PNG

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

Not true. If you have been following this post, the initial sample script was copied from an online article taken from a sample script to show how to rename a property of a JS object in which event I substituted property names and format and inserted/used double quotes (not doubled up single quotes). Once again, the quotes had nothing to do with the initial script not working as intended as I had also tried using both single and double quotes as opposed to doubled up single quotes already suggested in a previous response.  If you look at my last response, I provide the exact script I used to get this to work as explained. What I neglected to show or mention is that I had initiallty used a script for accessing nested JS object properties using oVendors[key][firstName] instead of oVendors[firstName] and given a space in the actual name, I needed to use the bracket notation with quotes, etc. However, this is a moot point at this time. All considered, it appears I could have better explained the entire post to begin with.  In reality, it was an easy fix but once again unrelated to quotes or pasting you alluded to in your response. In reality,  the script (shown below) provided in my initial post was created in a text editor using double quotes (not doubled up single quotes) and then copied/pasted into my post (same as done for the script taken from an online article) to save time. In fact, if you were to look closely at my initial script and yours, your double quotes appear to be wider apart than mine. Moreover, they appear to look more like the quotes provided in Nesa's response.

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

Marty, I think the quote issue is a translation(transcription) problem.  If you copy the code in the post you'll get doubled up single quotes.  You can even see it in the text selection. The cursor will select a single quote.  

Was this code copied from a word processing document? It may have been double quotes originally, but it's been converted to single quotes in the posts.

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 18, 2024 Jan 18, 2024

Copy link to clipboard

Copied

LATEST

In response to 'Was this code copied from a word processing document? It may have been double quotes originally, but it's been converted to single quotes in the posts', yes and no. All considered, I was at least able to resolve what turned ouit to be a simple fix regarding my script. Thanks again to all who responded. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines