title: How can I add elements to an Array of objects in Javascript?
body: <p>I am writting a program to get a list of cities and their GPS coordinates and store it in an Array  named &quot;cities&quot;, to worjk later with it. This is the fragment of code:</p>
  <pre><code>var city = {name:String,lat:Number,lon:Number};
  var cities = Array.of(city);
  
  city.name=&quot;London&quot;;
  city.lat=51.50;
  city.lon=-0,11;
  
  cities.push(city);
  
  city.name=&quot;Barcelona&quot;;
  city.lat=41.38;
  city.lon=2,15;
  
  cities.push(city);
  
  console.log(&quot;Number of cities &quot;+cities.length);
  console.log(cities);
  </code></pre>
  <p>The result when executing this code is the following:</p>
  <pre><code>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 }
  ]
  </code></pre>
  <p>That is, it seems that the &quot;push&quot; method <strong>overwrites all the array records</strong>, 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 &quot;city&quot;
  var cities = [];
  with both declarations the result is the same.</p>
  <p>That's very strange and it seems obvious, but I do not know what's happening..</p>
  <p>¿What am I doing wrong in the code shown above?</p>
  <p>Thanks in advance</p>