7. Line by Line


#1


https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#

I don't understand this line of the code:
if(friends[key].firstName === name)
can someone explain it to me and how it works?

var friends = {};
friends.bill = {
    firstName: 'Bill',
    lastName: 'lib',
    number: '(123) 456-7890',
    address: ['123 four street','las vegas','NV']
};
friends.steve = {
    firstName: 'Steve',
    lastName: 'eve',
    number: '(555) 555-5555',
    address: ['555 five road','las vegas','NV']
};
var list = function(object){
    for(var key in object){
        console.log(key);
    }
}
list(friends);

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


#2

In summary, if the particular key in the friends object in which other new objects were created, has a key, firstName, set that === name. The name is that argument your function would later receive when called.

=== equates both the value and data type

Note that each new object has key and values. key: 'value'

I hope I helped though! :slight_smile:


#3

add else{ return friends}


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.