Javascript - Google Chrome Cluttering Array Generated From .split()
Solution 1:
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.Solution 2:
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];
}
Solution 3:
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.
Solution 4:
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.
Solution 5:
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.
Post a Comment for "Javascript - Google Chrome Cluttering Array Generated From .split()"