I am working on a project and I am stuck at a point that I did not think was that hard. I have an array that looks like the following:
var Coords = [
{lat: 7, lng: 3},
{lat: 3, lng: 2}
]
I need to use a loop to push more lat and lng coordinates into this array...actually about 30,000 more coordinates. (Please note that the current data in there is an example.) I have tried doing:
Coords.push(Coords[2].lat = 100)
Coords.push(Coords[2].lng = 200)
and that does not help. Any help, with this simple problem would be great! I can figure out the loop just need to know how to get data in!!
2 Answers 2
Just use
Coords.push({lat: 1, lng: 2})
for pushing new data and
Coords[1].lat = 2
for modifying data.
Comments
Try:
Coords.push({lat: 100, lng: 200})
You can add multiple objects like that:
Coords.push({lat: 100, lng: 200}, {lat: 150, lng: 250}, {lat: 200, lng: 300})
.push()- not entirely sure what you're asking howeverCoords.push({lat: 100, lng: 200}).push(). Push will automatically place the inserted element at the end of your array. In other words, only use indices when you are updating/removing specific elements. Use the object when you are using thepush()function.