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?
Use
for x,y of L. Relevant documentation.Outputs
You may also want to consider the variant
for own k,v of agesas mentioned by Aaron Dufour in the comments. This adds a check to exclude properties inherited from the prototype, which is probably not an issue in this example but may be if you are building on top of other stuff.