this.someArray = [1,2,3,4,5]
for(var i in this.someArray){console.log(this.someArray[i]);}
It not only loops through the array elements but also junk like ‘remove’. I don’t want to abandon the enhanced loop and use the regular loop (as it is inconvenient). Is there a way to loop only through the array contents?
Your loop is catching the methods and properties of
Array.prototypein addition to the array’s contents. Like JCOC611 said, it is usually better to loop through arrays the “right” way, but if you don’t want to do that, you can use the array’shasOwnPropertyfunction to check if a property is actually in the array or if it is part ofArray.prototype:See this question for more details: Why is using "for…in" with array iteration a bad idea?
Also, this blog post has lots of information: http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/