Iterating over array of strings produces error

前端 未结 4 1302
太阳男子
太阳男子 2021-01-26 13:22

I pass in an array of error messages to parse. An example input would be:

\"An item with this x Id already exists.
 An item with this y id already exists.
 An it         


        
4条回答
  •  太阳男子
    2021-01-26 14:21

    You should use a regular for loop for arrays, because for-in will also return all the other property keys in the Array object. This is why you are seeing "hasObject" (and in my browser, I see a lot more after that): because your array has the function "hasObject", so when you enumerate all of Array's properties this comes up.

    The correct for-loop:

      for ( var i = 0, ii = aLines.length; i

    Here is your code with the for-in loop replaced with a for loop, and it works as expected:

    http://jsfiddle.net/SamFent/4rzTh/

提交回复
热议问题