Script to extract and assign values from a nested JS object

34 Views Asked by At

I presently have a nested JS object of the format:

var oNames = {
    Adobe:{LastUpdate:'03/09/2022',Website:'adobe.com',UserID:'[email protected]',PWD:'1234567',PrimaryCC:'',SecondaryCC:'',PrimaryBank:'',SecondaryBank:'',SQ1:'First girlfriend',SA1:'Denise',SQ2:'Grade school attended',SA2:'OLPH',Notes1:'',Notes2:''},
    Amazon:{LastUpdate:'10/06/2020',Website:'amazon.com',UserID:'[email protected]',PWD:'1234567',PrimaryCC:'Discover',SecondaryCC:'Capital One',PrimaryBank:'',SecondaryBank:'',SQ1:'',SA1:'',SQ2:'',SA2:'',Notes1:'',Notes2:''},
    AmericanGiant:{LastUpdate:'01/25/2022',Website:'american-giant.com',UserID:'[email protected]',PWD:'1234567',PrimaryCC:'',SecondaryCC:'',PrimaryBank:'',SecondaryBank:'',SQ1:'',SA1:'',SQ2:'',SA2:'',Notes1:'Best Hoodies Made in the USA',Notes2:''},
    Asus:{LastUpdate:'11/05/2022',Website:'https://www.asus.com/us/',UserID:'[email protected]',PWD:'1234567',PrimaryCC:'',SecondaryCC:'',PrimaryBank:'',SecondaryBank:'',SQ1:'',SA1:'',SQ2:'',SA2:'',Notes1:'Goto for MB & Laptop Computers',Notes2:''},
.
.
.
]

Can someone please provide me with a sample script that loops through the object above to obtain the values for the properties in the object above associated with each name using the variable, cName, that derives its value from a selection made by an end user from a drop-down box form field that resides on a PDF form. In short, the value provided by the variable 'cName' will be one to match any one of the name values in the object, i.e., Adobe, Amazon, AmericanGiant, Asus in which event the remaining fields residing on the PDF form will be populated with those property values associated with the name selected. Hope this explanation is clear. Unfortunately, I am not as familiar working with object pairs as I am with arrays and as such am stuck creating a script that works with a nested object as provided above. Thank you ahead of time.       
for(j=0;j<15;j++){
  f=getField("inf."+j);
  cName = oVendors[event.value][j];
}

"inf."+j denotes 1 of 15 PDF form fields to be populated from the values in the object associated
with the name in the object equal to 'cName', the name selected in the combo box that resides on the PDF form.

1

There are 1 best solutions below

0
AudioBubble On
Upon further study of the JS object and its methods, the following script is 
required  to extract names stored inside a JS data object in a hidden text field 
used to populate a drop-down box on the PDF form as follows:

dsFld =getField("dataSrc");// call getField method used to obtain the stored 
data value in the hidden text field that resides on the PDF form 
oVendors = JSON.parse(dsFld.value);// parse the JSON string to convert to a JS 
object to complete tasks to follow
f = getField("cbNames");// call the getField method to get the 
drop-down combo box field or later use
aNames=new Array();// create the temp array
for(var key in oVendors){// script to populate the temp array from names 
  taken from/stored in oVendors data object 
  aNames.push(key);
}
aNames.sort();// sort the names alphabetically in the temp array
f.setItems(aNames);// assign names from the array to the drop-down 
Combo Box field on the PDF form
f.insertItemAt("Add or lookup and select a name");// Insert text item at 
the topmost item provided in the drop-down combo box list
dsFld.value=JSON.stringify(oVendors);// convert JS obj back to a JSON 
string value stored in the hidden text field

Sorry for the initial ambiguous post and any inconvenience to the forum.