List 'em all!


#1

OutPut:
bill
steve
yogesh
{"firstName":"Yogesh","lastName":"Yadav","number":9,"address":["flat 203","Sea Side","Maxico"]}

Code:
var friends= new Object();
friends.bill={
firstName: "Bill",
lastName: "mathew",
number:1,
address:['Block 6','New York','Street 28','USA']
};
friends.steve={
firstName: "Steve",
lastName: "Johnson",
number:2,
address:['Sky Galaxy','Brazilia','Near President House']
};
friends.yogesh={
firstName: "Yogesh",
lastName: "Yadav",
number: 9,
address: ['flat 203','Sea Side','Maxico']
};

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

1.It is successfully running but, in the output , it is showing addition information of Yogesh's Address.

2, How it is taking automatically the first name of every friend?

Please Help.


#2

It isn't outputting the first name, but rather the name of the variable, look here.

//This is effectively the same as your code, and will help you see what I'm talking about
var friends = {
    'bill': {
        firstName: "Bill", //If you look at the first line outputted, you will see ^ bill, not <- Bill
        lastName: "mathew",
        number:1,
        address:['Block 6','New York','Street 28','USA']
    },
    'steve': {
        firstName: "Steve", //If you look at the second line outputted, you will see ^ steve, not <- Steve
        lastName: "Johnson",
        number:2,
        address:['Sky Galaxy','Brazilia','Near President House']
    },
    'yogesh': {
        firstName: "Yogesh", //If you look at the third line outputted, you will see ^ yogesh, not <- Yogesh
        lastName: "Yadav",
        number: 9,
        address: ['flat 203','Sea Side','Maxico']
    }
};

So, it isn't outputting the firstName of your friends, but rather the name of the Object that holds the info about each of them.