While implementing an internal EventEmitter
for a project I was working on, I came across a strange quirk when using Array.prototype.splice
inside a for... in
loop. The function does not successfully remove the indeces from the array within the loop:
var array = [1, 2, 3, 4, 5], index;
for (index in array) {
if (index === 2) {
array.splice(index, 1);
}
console.log(array[index]);
}
console.log(array);
Running on Google Chrome version 43, this outputs
1
2
3
4
5
[1, 2, 3, 4, 5]
when I'm expecting something like
1
2
4
5
undefined†
[1, 2, 4, 5]
Is this by design or a bug? I cannot find any documented reference to this behavior.
† Possibly, if length is not calculated during each iteration of for... in
implementation