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

There is a foreach that works: .forEach(). And when you're not just iterating over an array but transforming it, you have map(), reduce() and filter().


Is it really too much to ask to have some syntactic sugar. Is

    for i,v in blah
, where i is an index of an array, or a key (not a property!) of a dictionary really too much to ask in 2012?

No wonder people are using coffeescript.


CoffeeScript's got you covered. Here's forEach ...

    for item, index in list
... here's the same over an object ...

    for key, value of object
... here's the map() ...

    mapped = for item in list
... here's the filter() ...

    filtered = for item in list when item.isActive
... and so on.


Exactly. The number one thing javascript should take from coffeescript is the iteration syntax.


ecmascript 6 will also introduce for of loops which is said to fix for in. checkout wiki.ecmascript.org for all the ecmascript 6/harmony niceness




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

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

Search: