76

How can I use for (var key in object) in CoffeeScript? It compiles to...

for (_i = 0, _len = object.length; _i < _len; _i++) {
    key = object[_i];

...but I just want to iterate though an object.

fancy
  • 48,619
  • 62
  • 153
  • 231

2 Answers2

127

for key of object

Try it in js2coffee

Moox
  • 1,122
  • 9
  • 23
Raynos
  • 166,823
  • 56
  • 351
  • 396
72

of keyword:

for key, value of obj

or to make sure you're only checking properties on this object (and not the prototype chain):

for own key, value of obj
  • 1
    the point of that website is to a) see how I can do a js pattern in coffeescript and b) convert existing js files to coffeescript without re-writing them. – Raynos Jun 21 '11 at 23:36