During my coding tests, I came across this snippet of code:
<script>
var newData = {}, graphs = []
for(var j=0; j<2; j++){
newData["name"] = 'value '+ j
console.log(newData["name"]);
graphs.push(newData);
console.log(graphs);
}
</script>
When I checked the web console, I saw the following output:
value 0
Array [ Object ]
value 1
Array [ Object, Object ]
It's strange that all the objects in the arrays have the same values:
name:"value 1"
I find it puzzling because I'm not changing any values and the name is still being updated in the same loop.
I appreciate your insights on this issue!