I'm feeling a bit lost and frustrated at the moment. I'm attempting to initialize a JSON Array and dynamically add JSON Objects to it during runtime, but something isn't clicking for me.
Let's say I receive a list of repeated values from the server like this:
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
My goal is to create an array of objects and populate them with these values.
This is how I'm trying to start my array:
var myArr = [];
and then I want to add objects with properties to it dynamically, as shown below.
var myobj = {}
for(int i=0;i<length;i++)
{
myobj[i].name = serverrespOBJ.name;
myobj[i].lastName= 'serverrespOBJ.lastname';
myArr.push(myobj)
}
However, I keep getting an error saying name cannot be added to undefined, which leads me to believe I'm not adding items to the object correctly.
I've been searching for examples to help clarify things, but haven't had much luck. Any assistance would be greatly appreciated.
Thank you