7. Search For Friend: help understanding


#1

I've passed the lesson but I need help with understanding why.
Beneath is the code i've used and what is output. I understand everything apart from the search function. I get for/in loops but i think the for/in + if is confusing me somewhat. Any light shed would be greatly appreciated. Also if anyone could explain why the second contacts details are repeated that would also be great. THANKYOU

CODE:
var friends ={
bill:{
firstName:"Bill",
lastName:"Gates",
number:"07798739047",
address:['1 street avenue','Bedforshire','London']
},
steve:{
firstName:"Steve",
lastName:"Jobs",
number:"07789374181",
address:['2 maple drive','lincoln','Somerset']
}

};

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

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

OUTPUT:
{ firstName: 'Steve',
lastName: 'Jobs',
number: '07789374181',
address: [ '2 maple drive', 'lincoln', 'Somerset' ] }
{ firstName: 'Steve',
lastName: 'Jobs',
number: '07789374181',
address: [ '2 maple drive', 'lincoln', 'Somerset' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '07798739047',
address: [ '1 street avenue', 'Bedforshire', 'London' ] }


#2

You are calling the search function, passing it a name as a paramter. In the search function the for each loop will be entered (looping trough your friends variable). In the loop it checks if the firstname in the current position of the search variable, equals the variable name you've passed with since you called the function. If its true it displays alle the infos of the object. I don't think you'll understand this after you read this. But try to get familiar with object and foreach loops looping trough an array.


#3

can someone explain these two lines please.

for (k in friends){
if (friends[k].firstName === name){
^

why are we
writing this like an array