I have some example code:
var array = [];
array[0] = {id:0};
array[1] = {id:1};
array[2] = {id:2};
Now array looks like
[Object{id=0}, Object{id=1}, Object{id=2}]
I use splice:
array.splice(0,1);
And we have:
[Object{id=1}, Object{id=2}]
When I try for or for ... in length will be only 2 and I can’t iterate it in normal way. Result of loop is:
undefined
Object{id:1}
In first case (when we use for) I understand why it didn’t work, but for ... in should return indexes 1 and 2 not 0 and 1…
Anybody can explain me what I am doing wrong?
What did you try? If I issue
the engine correctly outputs
(but the array indices change to
0and1, perhaps that is what confuses you; remember thatArray.spliceby default removes from element0onwards and shifts all remaining elements downwards).Update: apart from the fact that you can always get the
idproperty of the individual objects usingarray[i].id, you can delete array elements without shifting the other elements using thedeleteoperator. After I replacearray.splice(0,1)withthe output shows