0

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!!

3
  • 1
    You'd just pass one of those object literals to .push() - not entirely sure what you're asking however Commented Aug 22, 2016 at 18:55
  • 2
    Like, Coords.push({lat: 100, lng: 200}) Commented Aug 22, 2016 at 18:56
  • You shouldn't use an index when using .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 the push() function. Commented Aug 22, 2016 at 18:57

2 Answers 2

6

Just use

Coords.push({lat: 1, lng: 2})

for pushing new data and

Coords[1].lat = 2

for modifying data.

Sign up to request clarification or add additional context in comments.

Comments

2

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})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.