I’m currently looping through an array of waypoints that are returned from the Google Maps API, in the following array I wish to access the values that are labelled Xa and Ya and at the moment I am doing it in this way – [i]["location"]["Xa"], however Xa and Ya are not always the same, sometimes it could be aB and aC or Ya and Za etc. Is there a generic way to access these values?
[
{
"location":{
"Xa":xx.xxxxxxxxxxxxx,
"Ya":xx.xxxxxxxxxxxxx
},
"stopover":true
},
{
"location":{
"Xa":xx.xxxxxxxxxxxxx,
"Ya":xx.xxxxxxxxxxxxx
},
"stopover":true
},
{
"location":{
"Xa":xx.xxxxxxxxxxxxx,
"Ya":xx.xxxxxxxxxxxxx
},
"stopover":true
},
{
"location":{
"Xa":xx.xxxxxxxxxxxxx,
"Ya":xx.xxxxxxxxxxxxx
},
"stopover":true
}
]
First of all, this is a javascript object (All associative arrays are actually objects in JavaScript).
If you are sure that the
locationobject will contain only 2 properties, you can use thefor ... instatement and be sure that the first element you access is the X Coordinate and the 2nd is the Y Coordinate.Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…in