6/8 Contact List


#1

I don't understand this completely. Can someone please help. Its asking me if I create a function. My brain hurts :frowning:

var friends = {
bill: {
firstName: 'Bill' ,
lastName: 'Johnson',
number: '908-778-9078',
address: ["768 Luoy Street"]
},
steve: {
firstName: 'Steve',
lastName: 'Brown',
number: '732-182-7890',
address: ['50 Ryan Road']
},
justina: {
firstName: 'Justina',
lastName: 'Oliva',
number: '732-564-5567',
address: ['102 Meerkia Street']
}
}
var list = function (friends) {
for (var friends in object) {
var friendOne = friends.bill.firstName;
var friendTwo = friends.steve.firstName;
var friendThree = friends.justina.firstName;
console.log(friendOne + " " + friendTwo);

}

}


#2

Consider a generic utility that only logs the primary keys of an object.

var friends = {
    bill: {},
    steve: {},
    justina: {}
};

bill, steve and justina are the primary keys. We can write our function and run it without having their respective objects populated.

var list = function (object) {
    for (var key in object) {
        console.log(key);
    }
};

list(friends);

Output:

bill
steve
justina

#3

...but when I do that, it says key is undefined. Do I have to define key as a variable somehow? Every time I see it in an example, it looks like it should be implying "for each key of this object" but I can't figure out how to actually do that.


#4

The variable key is the index as the loop iterates over the object.

object[key] // dynamic index

friends['bill']
friends['steve']
friends['justina']

Not sure why you are getting undefined. The code example above runs as expected.


#5

I see my mistake now thanks for explaining.


#6