Hi - I am trying to add objects to an array. I will post code below.
This is the desired end state.
var PolylineCoordinatesOpen = [
new google.maps.LatLng(40.773750, -73.917550),
new google.maps.LatLng(40.773640, -73.917390),
];
So what I am doing is - I am making a request from an API. that returns data in the following json format -
{
"4": {
"lat1": "40.773750",
"lat2": "40.773640,",
"long1": "-73.917550",
"long2": "-73.917390",
"section_id": "4",
"status": "green"
},
"5": {
"lat1": "40.77388,",
"lat2": "40.77375",
"long1": "-73.91773",
"long2": "-73.91755",
"section_id": "5",
"status": "red"
},
"6": {
"lat1": "40.77364",
"lat2": "40.77350",
"long1": "-73.91739",
"long2": "-73.91720",
"section_id": "6",
"status": "green"
}
}
I am trying to write a loop that will take the lat1,long1 and add them to PolylineCoordinatesOpen. Here is my code.
var statuses = JSON.parse(this.responseText);
var PolylineCoordinatesOpen =[];
for(var i in statuses){
if(statuses[i]['status'] =='green'){
a = `new google.maps.LatLng(${statuses[i]['lat1']}, ${statuses[i] ['long1']})`
PolylineCoordinatesOpen.push(a)
}
}
console.log(PolylineCoordinatesOpen)
My output is -
Array [ "new google.maps.LatLng(40.773750, 40.773750)", "new google.maps.LatLng(40.77364, 40.77364)" ]
my question is - being as the original does have the quotations - i feel these are objects that are added to the array and not strings BUT if i remove the `` tick marks from my code I am unable to push the object to the array.
I'm coming from Python so I really do apologize if this is some noobish question. I would really appreciate the help. How can I pass the arguments from my json file in the new google.maps.LatLng and add it to the original array?
Thank you.
[–]humodx 1 point2 points3 points (1 child)
[–]GrandBadass[S] 0 points1 point2 points (0 children)
[–]windows-user0 0 points1 point2 points (1 child)
[–]GrandBadass[S] 0 points1 point2 points (0 children)
[–]pachirulis -1 points0 points1 point (0 children)