I am running this code and checking the log in firebug:
var a = new Array();
var b = new Array();
for (i=0; i<2 ; i++){
a.push(1);
b.push(a);
console.log("a", a);
console.log("b", b);
};
The log shows these values:
a [1]
b [[1]]
a [1,1]
b [[1,1],[1,1]]
According to me the values should be:
a [1]
b [[1]]
a [1,1]
b [[1],[1,1]]
What am I doing wrong and how could I get the values that I want?
When you’re using
.pushto appendato the end ofb, you’re passing a reference and not ByVal as an Array is an Object. This means that future changes toaare reflected in theas already inb.What you need is to
.sliceawhen.pushing tob.