javascript splice skipping element- why this behaviour

后端 未结 3 890
梦如初夏
梦如初夏 2021-01-29 06:07

I was working with splice within a nested for loop and I came across a behaviour I could not understand.

var a = [0, 1, 2, 3, 4];

for (b in a) {
           


        
3条回答
  •  遇见更好的自我
    2021-01-29 06:36

    After you remove element 1, then "a" looks like:

    [0, 2, 3, 4]
    

    The new element 1 is 2. The next iteration of the loop, then "c" is 2, and a[2] is indeed 3.

提交回复
热议问题