How to iterate over the keys and values in an object in CoffeeScript?

I have an object (an “associate array” so to say – also known as a plain JavaScript object):

obj = {}
obj["Foo"] = "Bar"
obj["bar"] = "Foo"

I want to iterate over obj using CoffeeScript as follows:

# CS
for elem in obj

bu the CS code above compiles to JS:

// JS
for (i = 0, len = obj.length; i < len; i++)

which isn’t appropriate in this case.


The JavaScript way would be for(var key in obj) but now I’m wondering: how can I do this in CoffeeScript?

4 Answers
4

Leave a Comment