How var key in Object works?


#1



1.how var key in friends works? I dont know how the incrementation works for this case
2.why i cant subtitude friends[key] with friends.key?


var search = function(name) {
    for(var key in friends) {
        if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        }
    }
};


#2

Click here read the stetim94's last comment, I think this will help.


#3
  1. the for .. in .. loop does all the work for you, each iteration the key gets assigned a object (steve, bill) from your friends object

  2. because your friends object contain another object (steve, bill) so you need the square brackets notation. Then you can use the .firstName to access firstname property of your steve or bill object (depending where in the loop you are)

Combined with my other answer (which was already linked to), it should explain almost everything, do ask if you have any further questions


#4

I would also add for no.2:

friends.key is not mentioning it is just like friends.bill, friends.david for the object friends. So, it does not work in console.log();. You can even create an object inside friends like friends.key. As, there is no friends.key object in this exercise; it's like you are trying to create an object for friends object inside console.log() which should not work. However, you can console.log(friends.bill) which will only give output for that specific object.

however, friends[key] is mentioning/addressing all the key of friends in an array which then through console.log() gives us output.


#5

i want to ask
1.start from this for(var key in friends)
this copy object value to var key and we can only loop in an object,we are not able to loop in friends properties(let say in friends.bill.name),right?

2 . so phase 1 copy bill to var, it will loop till the end of bill properties then go to phase 2,copy steve to var and then loop again till the end of steve prooperties, right?

3.so when i assign value to key, i assign object of friends,lets say bill
so key now has a value of bill....... friends[key] now has the same meaning with friend["bill"],which is enable me to access the properties of bill,so i thought friends["bill"]== friends.bill....am i wrong?
still cant understand the reason behind this array vs using .

4.if so, is there a way to start looping from specific property ( example from bill property number two, lets say friens.bill.age) using for in?


#8

I am giving you what I think is correct. Please help if somebody can check any problem in my explanation.

Question 1. for(var key in friends) : You are accessing all the properties(key + value). However, for loop does not end there.

                If you write console.log(friends[key]) it will access the values.
                If you write console.log(friends[key].age) it will specify which value (in this case age).

I would appreciate if someone else also check this.


#9

we can simply test:

for(key in friends){
    for(property in key){
        console.log(key[property])
    }
}

nope, it will just give you the letters of bill and steve property's.

nope, var just indicates you have a local variable (it only exist inside the object), it copys bill to key. For the rest, point 2. is correct

  1. yep, key is just a variable which will hold "bill" and "steve". It has been a while, not sure why we use [] and not .

If i had to take a guess, i would say the for in loop is to blame. Because you can use both the dot:

console.log(friends.bill.lastName)

and the brackets:

console.log(friends["bill"].number)

if you don't have a for loop. So, i guess the for loop is to blame, because in the for loop, key is a variable which holds a string, which means you need the brackets, and without a for loop you refer to the object directly, which allows you to use the dot notation .

  1. Not going to answer this yet, that is roughly what you will do in the next exercise (or the one after that)