The specific value of the parameter in for-in loop


#1


https://www.codecademy.com/courses/objects-ii/1/5?curriculum_id=506324b3a7dffd00020bf661#


My code is not error, I came here because I am curious about other thing that came across in my mind about the object lesson so I did some test. I don't know if I can ask in here, if I cannot then I am sorry.
I just tried to create Object using different notation and see the outcome when use for-in loop. And then I saw than the 'x' outside of the for-in and inside gave me different values. I would like some explanation.

Also, what if I want the for-in to console.log only the value of some particular parameter: how can I do that? (or it is not possible for the for-in loop)

If there is already a lesson or explanation about this somewhere else, please provide me the link. Thank you!


var dog = new Object();
dog.spe="b";
dog.age=5;
console.log(dog.spe); //this get b

dog["spe"]="c";
console.log(dog.spe); //get c
console.log(dog["spe"]);//get c 

var x= "spe";
dog["spe"]="W";
console.log(dog[x]); //get W

for(var x in dog){
    console.log(dog[x]); //x give me spe, and age. x here is different. why? what if I want the value of an x only?
}


#2

@tuangt,

T h e - B a s i c s

An object has one or more properties separated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties separated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#3

Thank you very much. ^^ However, I have finished the code followed the instruction of the lesson without a problem. I understand the x , and nyc[x] in the lesson. ( at least I guess I understand what they are used for)
Anyway, I would like to make sure I understand your explanation correct.

Follow what I coded to test, the lastest variable 'x' is W. And you explained that because the 'x' in For-In-Loop is used to find the property-key so this is not the same valuable x outside of the Loop. They are different 'x' at the first place. Is this correct?

Also, because the Loop is the Loop so it will run through every properties in an object, which means I cannot lock only the thing I want to display. For example, the dog[x] can give me the W, but if I put in this loop it will always gives me the W, and 5. Is this correct? - if not, is there a way to get the particular value using for-in-loop. Is it impossible? (If this possible, and it is difficult to do, like it needs an advance skill, I totally understand. I am just curious what is capable of.)

Thank you again for your reply. :smiley:


#4

@tuangt,
In your test's
use
console.log(dog);


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.