Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> I don't think that allowing you to iterate over a hash as if it were an array is all that unforgivable

it's not even doable in JavaScript: bare objects (~hashes) are iterated with for...in, using that with arrays gives inconsistent results: the array's keys will be iterated as if they were (string) properties, there are no guarantees they'll be iterated in numerical order, and any enumerable property added to the array itself or any of its ancestors will be iterated over as well.

Friends don't let friends iterate over arrays with for...in.



> Friends don't let friends iterate over arrays with for...in.

Which is too bad because it looks so much better.


I just use Array.prototype.[forEach|map|filter] instead, or the equivalent Underscore.js function (which aliases to native when it can) if IE-compatibility is needed.

If you need performances, nothing will beat C-style access anyway. And FWIW, using for...in on an array is slower than using Array.prototype.forEach.

edit: FWIW, even hand-rolling an each() function (in the style of underscore's) will be faster than for...in.


it works this way, though, because an array's keys actually are string properties. It's the array methods that treat them as if they were numeric.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: