top_obj = { nest_obj1: { prop: 'value' }, nest_obj2: { prop: 'another value' } } for(key in top_obj){ console.log(key.prop); // undefined } console.log(top_obj.nest_obj1.prop); // а так - всё нормально 

How do I get to the attached property in a loop?

    2 answers 2

    Properties can still be accessed through parentheses.

     top_obj['nest_obj1'] 

    It turns out we can transfer a variable to the brackets. And the key in the loop is the key of the object, not an embedded object, so you need to

     top_obj[key] 

      The key loop variable is not a pointer to a property; it is a string containing the name of the property.

       top_obj = { nest_obj1: { prop: 'value' }, nest_obj2: { prop: 'another value' } } for (key in top_obj) { console.log(top_obj[key].prop); }