Wednesday, 21 August 2013

Javascript push Object to global array overwrites previous values?

Javascript push Object to global array overwrites previous values?

I'm having this problem in Javascript: I want to get the longitude and
latitude values from an array of Objects. This all works fine, but when I
try to save it into a global array, it overwrites every previous value.
The result is an array with 8 times the last pushed object.
Global array: var _coordinates = [];
function getCoordinates()
{
mark = {};
for(var key in _data)
{
if(_data.hasOwnProperty(key)){
mark["lng"] = _data[key].long;
mark["lat"] = _data[key].lat;
}
console.log(mark); // Returns different coordinates (yay)
_coordinates.push(mark);
}
console.log(_coordinates); // All coordinates are the same (meh)
}
This is my first time asking a question here. So if I forgot something,
please say so.

No comments:

Post a Comment