var cb = [];
for (var i = 0; i < 10; i++) {
cb.push({
'test': 'value'
});
console.log(cb);
};
I'm expecting to get: [{test: value}, {test: value}, ... , {test: value}]
What I end up getting is the final result at every log statement:
[Object]
[Object, Object]
[Object, Object, Object]
[Object, Object, Object, Object]
[Object, Object, Object, Object, Object]
[Object, Object, Object, Object, Object]
[Object, Object, Object, Object, Object, Object]
..........
When I expand any of those arrays they all have the same result. For example, the first array contains:
[{test: value}, {test: value}, ... , {test: value}]
which is the final value, shouldn't it just have 1 object? The final result is what I expect, but I'm just confused about why after the first push the array has 10 elements. Can someone please explain what's going on?
console.logshows the state ofcbnow, and not at the time it was logged.console.log(JSON.stringify(cb))and you'll see what @Siguza is saying. Or evenconsole.log(cb.length)will show you that the length is increasing correctly.