Y
Y
Yaroslav IKARUS2015-11-11 13:07:25
CoffeeScript
Yaroslav IKARUS, 2015-11-11 13:07:25

How to iterate over an object in coffeescript?

There is an object

moscow = 
    "cao": 4500
    "yua": 4500
    "yuvao": 4500
    "vao": 4500	
    "svao": 3800
    "sao": 3500
    "szao": 3800
    "zao": 4500
    "yuzao": 4500

You have to cycle through it.
Tried doing it like this
for key in moscow
    if key == currentRegion
      alert "Совпадение"

It compiles to this
results = [];
    for (i = 0, len = moscow.length; i < len; i++) {
      key = moscow[i];
      if (key === currentRegion) {
        results.push(alert("Совпадение"));
      } else {
        results.push(void 0);
      }
    }
    return results;

And shit doesn't work.
The debugger shows the following
7f0f4e215a.jpg
That is, for some reason , len is undefined
Along the way, I'm doing something wrong. Maybe there are some more correct methods of how to do what I need for Coffee?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yaroslav IKARUS, 2015-11-11
@yar-ikarus

Thank you all very much, I figured it out myself.
In short, this is how it works

for key of moscow
    if key == currentRegion
      alert "Совпадение"

That is, we change the word in to of and immediately everything is nishtyak

V
Viktor, 2015-11-11
@master2016

Read about the length of js objects (you still need to know this). You will be as surprised as I was the first time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question