7. Search For Friend: help understanding


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

var friends ={
address:['1 street avenue','Bedforshire','London']
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){
return friends[key];

{ 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' ] }


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.


can someone explain these two lines please.

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

why are we
writing this like an array