for..in loop loops over non-numeric indexes “clean” and “remove”

后端 未结 3 516
醉酒成梦
醉酒成梦 2021-01-29 10:10

This is something very basic I might be missing here but I haven\'t seen such result till now.

I have a for loop where options.headers.length is 3. And in

3条回答
  •  一整个雨季
    2021-01-29 10:27

    I assume that options.headers is an Array?

    This happens when you (or some framework you load) adds methods to the Array prototype. The "for in" loop will enumerate also these added methods. Hence you should do the loop for an array with:

    for (var i = 0; i < options.headers.length; i++)
    

    That way you will only get the real values instead of added methods.

提交回复
热议问题