Introduction to Objects II 10. List ALL the properties


#1



Hello! 
plz help me. I dont understand why it doesn`t works. I dont want to use 
this syntax "console.log(nyc[x])" I want to use syntax console.log (nyc.x)

var nyc = {
    fullName: "New York City",
    mayor: "Bill de Blasio",
    population: 8000000,
    boroughs: 5
};
**console.log (nyc.fullName);**  Here it work, as a result i have "New York City". 
But when i try to
 make for in loop it doesn`t work. As a result i have undefined, undefined, 
undefined, undefined

for (var property in nyc) {
    
    console.log (nyc.property);
    };


#2

We cannot use dot notation with a variable. Use subscript (array) notation:

    console.log(nyc[property]);

#4

var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};
console.log (nyc.fullName); Here it work, as a result i have "New York City".

but what about this case? it`s not a variable?


#5

nyc.fullName is called an identifierName, and in its present form refers to a defined object, the fullName property of nyc. There is no defined property, property so we cannot write, nyc.property.

With a for..in statement, the iterator variable takes on each property name, in turn, in any order (objects are not ordered).


#6

When you access one value of a property you use:

A. console.log(nyc.fullName); or cosole.log(nyc["fullName"]);

When you use for(var x in nyc) you want to access all the properties in nyc like an array so you use:

B. console.log(nyc[x]);

No.A is for to access single property value, no.B is for an array. As for...in loop behaves like counting in an array, we use no.B

That's how I see it. Might have some other good explanation. Note the difference [X] and ["X"].


#7

fellos, thanks a lot! You have change my programming world :slight_smile:
For all this course i could not understand why i had to use (nyc[x]) syntax. Now i know why.
I wasted hours in other exercises trying to understand why nyc.fullName) syntax doesn`t work. I should have asked this earlier
Sorry for my english))