How to pass parameters to an array in javascript?

Asked

Viewed 476 times

5

How could I dynamically pass the information to the array down below?

Example: I have a 40 coordinates listing in a text file, for example, or in a database Access, how do I pass these coordinates to the variable locations down 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}
];
  • 1

    vc would have to show the source file format. Basically vc will use a get and loop.

2 answers

7

If the object has already been created you can access using the array’s input and the key you want to access.

Example:

locations[0].lat = -51.00;

to access the first element of Locations.

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

Example:

locations.push({lat: -51.00, lng: 29.00});

5


You can pass the coordinates inside the Locations variable as follows:

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

Updated

Through the resource push you will add the item at the end of the Locations (array) variable. It is worth noting that you must make sure that you are correcting the object within the function push. Example:

{
   lat: 10.00,
   lng: 20.00
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.