i have a JSON object something like;
var data = {
Name : "test name",
Type : "test type",
VendorInfo :{
FirstName : "Vendor First Name",
LastName : "Vendor Last Name",
Address : "Vendor Address",
City : "Vendor City",
ZipCode : "Zip"
}
}
now separately i want to keep info on how to access the fields in the data var
var accessInfo=[
{ fieldName : "Name", actionName : "Edit" },
{ fieldName : "Type", actionName : "Edit" },
:
:etc..
]
now in my javascript i can iterate over the accessInfo array by a for loop, something like;
for(var i=0;i<accessInfo.length;i++){
data[accessInfo[i].fieldName] = "changed field";
}
that works fine for top level fields, but i cannot figure out how to access the fields lower down in the hierarchy.. like data.VendorInfo.FirstName, data.VendorInfo.LastName etc.
Is this at all possible? i tried putting “VendorInfo.FirstName” and [VendorInfo][FirstName], but no go..
You just need to parse your access specification to traverse the target object. You can use a function like this: