I have the following object array in which I'm storing bus route by number and then by name:
var routearray =[
ruta01 =
{
via01: "Progress",
via02: "Ten",
via03: "S",
via04: "Maria"
},
ruta02 =
{
via01: "Exterior",
via02: "Interior"
},
ruta03 =
{
via01: "University",
via02: "Henry St"
},];
And I have the following code with which I want to access the value of each property:
for(i=0;i<routearray.length;i++) //iterates through the array's objects
{
var props = Object.keys(routearray[i]); //array that stores the object properties
for(j=0;j<props.length;j++) //iterates through specific object properties
{
console.log(props[j]); //shows the property names
var propertystring = String(props[j]); //transforms the property name into string
console.log(routearray[i].propertystring]; //should access the property value
}
}
When the last line of the code executes, I get 8 "undefined" results in the console. If I change it to something like:
console.log(routearray[i].via01];
It works just fine, but I'm not sure why it is not accessing the value if the string is supposed to work correctly. What am I doing wrong? Is there a better way?