i am facing a strange behaviour in a for loop that contains array of objects
The following is the example
var store = {};
var storesWithTimestamps = [];
for (var i=0;i<2;i++){
console.log("inital list",storesWithTimestamps); //1
console.log("inital length",storesWithTimestamps.length); //2
store = {};
store.latestTimestamp = null;
store.storeName = "testStore";
storesWithTimestamps.push(store);
console.log("new list",storesWithTimestamps); //3
console.log('new length',storesWithTimestamps.length); //4
}
The problem is log statement 3 shows a array of object with 2 items in the 1st iteration , but the log statement 4 shows the length as 1.
The output of the log statement 3 is the same for both the iteration like this, [{latestTimestamp:null,storeName:"testStore"},{latestTimestamp:null,storeName:"testStore"}]
Where as it should be 1st loop:
[{latestTimestamp:null,storeName:"testStore"}]
2nd loop:
[{latestTimestamp:null,storeName:"testStore"},{latestTimestamp:null,storeName:"testStore"}]
FYI: this works as expected in Safari but not on chrome - OSX Attached fiddle: http://jsfiddle.net/gauravsoni/09Ls3rtx/