0

I'm just starting to learn coding, and i came across this question that i could not understand.

"The second function we'll add will be called search, and it will take a first name as an argument. It will try to match the first name it receives to any of the first names in our friends contact list. If it finds a match, it will log our friend's contact information (firstName, lastName, number, address) to the console."

variables are define as follows :

var friends = {}; 
friends.bill = { 
  firstName: "Bill", 
  lastName: "gates", 
  number: "1234567", 
  address: ['bishan','starbucks', 'centertable'] 
}; 

friends.steve = { 
  firstName: "Steve", 
  lastName: "jobs", 
  number: "987654", 
  address: ['orchird', 'ikoma', 'ga'] 
};

the answer is as follows :

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

could someone better explain how did the var "key" came about ? and why can't i just input friends.firstName === name, console.log(friends.name), return friends.name ??

would appreciate if someone could explain thank you.

5
  • review official object in mdn developer.mozilla.org/es/docs/Web/JavaScript/Referencia/… Commented Feb 23, 2017 at 9:06
  • what does friends look like? what is the output of console.log(friends[key]); ? Commented Feb 23, 2017 at 9:07
  • var friends = {}; friends.bill = { firstName: "Bill", lastName: "gates", number: "1234567", address: ['bishan','starbucks', 'centertable'] }; friends.steve = { firstName: "Steve", lastName: "jobs", number: "987654", address: ['orchird', 'ikoma', 'ga'] }; Commented Feb 23, 2017 at 9:11
  • above is how the object friends was presented. (#am reading the moz file) :) ty Commented Feb 23, 2017 at 9:11
  • @DarkArtistry Please add this:- var friends = {}; friends.bill = { firstName: "Bill", lastName: "gates", number: "1234567", address: ['bishan','starbucks', 'centertable'] }; friends.steve = { firstName: "Steve", lastName: "jobs", number: "987654", address: ['orchird', 'ikoma', 'ga'] }; to your question as it is important for the people who would like to answer. Commented Feb 23, 2017 at 9:26

2 Answers 2

1

From OP's comment:

var friends = {}; 

friends.bill = { 
  firstName: "Bill", 
  lastName: "gates", 
  number: "1234567", 
  address: ['bishan','starbucks', 'centertable'] 
}; 

friends.steve = { 
  firstName: "Steve", 
  lastName: "jobs", 
  number: "987654", 
  address: ['orchird', 'ikoma', 'ga'] 
};

friends is a nested object which can also be represented like so:

friends = {
  bill: { 
      firstName: "Bill", 
      lastName: "gates", 
      number: "1234567", 
      address: ['bishan','starbucks', 'centertable'] 
  },
  steve: { 
      firstName: "Steve", 
      lastName: "jobs", 
      number: "987654", 
      address: ['orchird', 'ikoma', 'ga'] 
  }
}

The for..in loop iterates over all keys in the friends object, with the variable key in your case.

why can't i just input friends.firstName === name, console.log(friends.name), return friends.name ??

Because, to do that, you need to have firstName or name as a property in friends. Since those properties are nested inside (name is not event inside the nested objects), there was a for..in loop used.

Sign up to request clarification or add additional context in comments.

Comments

1

You have an object friends that has 2 properties bill and steve (those are the keys). Calling friends.bill will return you an object (the value) with firstname, lastname, number, address. You need to iterate all the properties of your object friends to find the one you need

You can use Object.values(obj)

var firstNameInput = "Steve";
var friends = {}; 
friends.bill = { 
  firstName: "Bill", 
  lastName: "gates", 
  number: "1234567", 
  address: ['bishan','starbucks', 'centertable'] 
}; 
friends.steve = { 
  firstName: "Steve", 
  lastName: "jobs", 
  number: "987654", 
  address: ['orchird', 'ikoma', 'ga'] 
};

//Iterates all the friends
Object.values(friends).forEach(function(f){
  //Compare the property "firstname" with the input
  if(f.firstName === firstNameInput){
    //Friend found
    console.log(f);
    return;
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.