How to pass parameters to an Array in javascript?

5

How can I dynamically pass information to array below?

Example: I have a list of 40 coordinates in a text file, for example, or in a Access database, how do I pass these coordinates to the variable locations below? >

var locations = [
        {lat: -31.563910, lng: 147.154312},
        {lat: -33.718234, lng: 150.363181},
        {lat: -33.727111, lng: 150.371124},
        {lat: -33.848588, lng: 151.209834},
        {lat: -33.851702, lng: 151.216968}
];
    
asked by anonymous 09.11.2016 / 01:11

2 answers

5

You can pass the coordinates into the locations variable as follows:

locations.push(
   { lat: -30.851700, lng: 150.363100 }
);

Updated

Using the push feature, you will add the item to the end of the locations (array) variable. It is worth noting that you should make sure that you are passing the object within the push function. Example:

{
   lat: 10.00,
   lng: 20.00
}
    
09.11.2016 / 01:26
7

If the object has already been created you can access it using the array index and the key you want to access.

Example:

locations[0].lat = -51.00;

to access the first locations element.

To add new elements use push and it will add at the end of your array.

Example:

locations.push({lat: -51.00, lng: 29.00});
    
09.11.2016 / 01:24