I am writting a program to get a list of cities and their GPS coordinates and store it in an Array named "cities", to worjk later with it. This is the fragment of code:
var city = {name:String,lat:Number,lon:Number};
var cities = Array.of(city);
city.name="London";
city.lat=51.50;
city.lon=-0,11;
cities.push(city);
city.name="Barcelona";
city.lat=41.38;
city.lon=2,15;
cities.push(city);
console.log("Number of cities "+cities.length);
console.log(cities);
The result when executing this code is the following:
Number of cities 3
[
{ name: 'Barcelona', lat: 41.38, lon: 2 },
{ name: 'Barcelona', lat: 41.38, lon: 2 },
{ name: 'Barcelona', lat: 41.38, lon: 2 }
]
That is, it seems that the "push" method overwrites all the array records, and also add an additional record. I also have declared the array cities as follows: var cities = [city]; // An array of elements of the class "city" var cities = []; with both declarations the result is the same.
That's very strange and it seems obvious, but I do not know what's happening..
¿What am I doing wrong in the code shown above?
Thanks in advance