E
erdibalint
Hi,
I've come across the problem and I spent a remarkable amount of time
debugging at the end of which I found the cause of the bug to be the
following:
var arr = ['a', 'b', 'c', 'd'];
var arr2 = [];
for (var i=0; i < arr.length; i++) {
var elt = arr;
arr2.push(elt);
}
// arr2 equals ["a", "b", "c", "d"]
var arr = ['a', 'b', 'c', 'd'];
var arr2 = [];
for (var i in arr) {
var elt = arr;
arr2.push(elt);
}
// arr2 equals ["a", "b", "c", "d", function()]
I thought that the second type of for loop is identical to the first
one and just adds syntetic sugar. I guess I was wrong, can anyone shed
some light on how the two loops are different?
Thank you,
Bálint
I've come across the problem and I spent a remarkable amount of time
debugging at the end of which I found the cause of the bug to be the
following:
var arr = ['a', 'b', 'c', 'd'];
var arr2 = [];
for (var i=0; i < arr.length; i++) {
var elt = arr;
arr2.push(elt);
}
// arr2 equals ["a", "b", "c", "d"]
var arr = ['a', 'b', 'c', 'd'];
var arr2 = [];
for (var i in arr) {
var elt = arr;
arr2.push(elt);
}
// arr2 equals ["a", "b", "c", "d", function()]
I thought that the second type of for loop is identical to the first
one and just adds syntetic sugar. I guess I was wrong, can anyone shed
some light on how the two loops are different?
Thank you,
Bálint