4

Given the following string:

var str = "one,two,three";

If I split the string on the commas, I normally get an array, as expected:

var arr = str.split(/\s*,\s*/);

Trouble is that in Google Chrome (for Mac), it appends extra properties to the array.

Output from Chrome's debugger:

arr: Array
    0: one
    1: two
    2: three
    constructor: function Array()
    index: undefined
    input: undefined
    length: 3

So if I iterate over the array with a for/in loop, it iterates over the new properties. Specifically the input and index properties. Using hasOwnProperty doesn't seem to help.

A fix would be to do a for loop based on the length of the Array. Still I'm wondering if anyone has insight into why Chrome behaves this way. Firefox and Safari don't have this issue.

user113716
  • 318,772
  • 63
  • 451
  • 440
  • @KennyTM: why not `str.split(',');`? – Andy E May 26 '10 at 16:00
  • 3
    It's pretty clear patrick is doing that to remove whitespace padding from the items in the resultant array. – Peter Bailey May 26 '10 at 16:02
  • 3
    @Peter: indeed, my comment was aimed at KennyTM as I'm not sure why you would use a regex for splitting on a comma only. – Andy E May 26 '10 at 16:05
  • So it sounds like `for/in` over an array is bad. Point taken. Still curious about why Chrome would try to iterate `input` and `index`. Or why they're there at all. – user113716 May 26 '10 at 16:06

5 Answers5

11

Don't iterate over arrays using for...in loops!! This is one of the many pitfalls of Javascript (plug) - for...in loops are for iterating over object properties only.

Use normal for loops instead.

for (var i=0, max = arr.length; i < max; i++) { ... } 


Firefox and Safari's ECMAScript/Javascript engines make those particular properties non-enumerable ({DontEnum} attribute), so they would not be iterated over in a for...in loop. Still, for...in loops were not intended to iterate over array indexes.
Community
  • 1
  • 1
Andy E
  • 338,112
  • 86
  • 474
  • 445
3

For..in is for iterating over enumerable properties of objects. For an array, to iterate over its indicies, just use a standard for loop

for ( var i = 0, l = arr.length, i < l; i++ )
{
  // do whatever with arr[i];
}
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
2

Not directly relevant to this particular problem, but note that splitting strings with regular expressions has all sorts of cross-browser issues. See http://blog.stevenlevithan.com/archives/cross-browser-split for more info, and for solutions.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

Iterating over an array with a for/in loop is not recommended, in general. First of all the order of iteration is not guaranteed, and in addition, you risk issues like the one you are having. You are better off using a traditional for loop.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
0

I assume that using a regexp here results in an array-like object, much like the result of the execution of RegExp.exec().

I could not reproduce the bug on Chrome/Win7, but I suggest to use the Array.prototype.slice.call(arr) magic. It is known for perfectly turning array-likes into real arrays.

Lapple
  • 3,385
  • 20
  • 20