Not sure how to print the contents of my object


#1

var friends = {};
//adds objects inside friends object
friends.bill = {
firstName: "Bill",
lastName: "Last",
number: "313-224-1212",
address: ["The block rd","Detroit", "MI", "48223"]
};
friends.steve = {
firstName: "Steve",
lastName: "First",
number: "313-323-4545",
address: ["Down rd","Dearborn", "MI", "48020"]
};

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


#2

@dhunter90 - you have:

for(var friends in object)

but with the for/in loop, it should be:

for(var key in object)

where "key" is the property that changes, and "object" is the name of the object. So it should look more like:

for(var key in friends)

and where you have the console.log statement, instead of logging the whole friends object, log that particular value. Then, you'll need to call your function at the bottom.


#3

@freginold I'm also having an issue with printing out the contents of the friends object. I can't seem to access the bill and steve objects inside of friends..Here is what I have.

var friends = {};
//adds objects inside friends object
friends.bill = {
firstName: "Bill",
lastName: "Last",
number: "313-224-1212",
address: ["The block rd","Detroit", "MI", "48223"]
};
friends.steve = {
firstName: "Steve",
lastName: "First",
number: "313-323-4545",
address: ["Down rd","Dearborn", "MI", "48020"]
};
var list = function()
{
for(var key in friends)
{
console.log(friends.bill);
};
for(var key in friends)
{
console.log(friends.steve);
};
};
list();


#4

@dhunter90 are you getting an error message? when I copy your code and run it word for word, it works fine, and prints out the properties and values for both Bill and Steve.


#5

@freginold Hi, i have tried the for/in loop a variety of ways including the one posted above and i'm getting an erro message saying "It looks like your function isn't printing the right output". I don't understand what to log... thanks for the help!


#6

Did the exercise tell you to print out the values for both Bill and Steve? A lot of the time you just have to make sure you're printing out exactly what they want you to print, word for word. Also make sure you have anything capitalized (or not capitalized) if it's supposed to be.


#7

Hi thanks for your reply @freginold ! My problem was it was printing out everything within the objects "bill" and "steve" and not just their names. I solved this by using ;

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

It did print "bill,steve,bill,steve" but it said it was correct!
Hope this helps for anyone else stuck!


#8

Glad it works! You could also try something like

console.log(friends[key].firstName)

to print the values stored as each one's first name.


#9

Just had been following this thread. Dear , what about to access the address,

I tried the code below but it would only give the first element of the array . What if i want the two elements in the array?? Example the array has a value ["Mclean",505]
var list = function(){
for(address in friends){
console.log(friends[address].address[0] );
};
};
list();


#10

@dazmera something like that should work. Have you tried running the code? If so and it didn't work, what error did you get? You might want to put a var in your for/in loop before address.


#11

Hey yeah the exercise seems to want to print out all the properties in the friends object..which are the bill and steve object. Printing these objects seems to give a "function isn't printing right output" error message. I tried just printing their first names that didnt work. Then printed each object. Got the same message. What am i missing?


#12

I think from what I remember of that exercise, they only want you to print out one of the friends object properties, but I can't remember if it's supposed to be Bill or Steve.