1

This is the second time I'm going through the Javascript lesson in codecademy as a refresher and there's something I don't quite understand. In on exercise called 'Contact List Project', the following is supposed to be the end code as provided by the lesson for viewing example.

var friends = {};
    friends.bill = {
      firstName: "Bill",
      lastName: "Gates",
      number: "(206) 555-5555",
      address: ['One Microsoft Way','Redmond','WA','98052']
    };
    friends.steve = {
      firstName: "Steve",
      lastName: "Jobs",
      number: "(408) 555-5555",
      address: ['1 Infinite Loop','Cupertino','CA','95014']
    };

    var list = function(obj) {
      for(var prop in obj) {
        console.log(prop);
      }
    };

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

    list(friends);
    search("Steve");

The code produces the following result in the console:

bill
steve
{ firstName: 'Steve',
  lastName: 'Jobs',
  number: '(408) 555-5555',
  address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","95014"]}

How exactly does the for/in loop work? I understand that is just a placeholder but why does list(friends) come up with just 'bill' and 'steve'? Isn't it supposed to run through all the properties of an object?

0

2 Answers 2

0

It is running through all of the properties of the friends object. This object only has 2 properties, bill and steve as defined here:

var friends = {};
    friends.bill = {
      ...
    };
    friends.steve = {
      ...
    };

All of the sub-properties within these two are not relevant to the initial for/in loop. The search function is what prints them out once it finds the one with firstName === 'steve'

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

4 Comments

Codecademy refers to 'bill' and 'steve' as keys to the object i.e. obj.[key] and it refers to it's sub-properties as 'properties'. Are these terms (keys, properties, sub-properties) interchangeable or do they have specific definitions that I'm just not getting? If you could shed a little bit of light in that, thank you in advance.
properties are made up of a key and a value. here bill is a key and steve is a key and the objects they are equal to are their values.
Thank you that helped. One last question, why does the code need to have list(friends); in order for search("Steve"); to run? Aren't those functions independent of each other?
It doesn't! it's just there to show you the properties. If you comment out the list(friends) line you'll see that search works just fine. list() is simply printing them to console (or "listing" them) for you to see
0

I think an in-depth look at Object.defineProperty() described here will clear your confusion about property, key & value.

Object.defineProperty(obj, 'key', {
  enumerable: false,
  configurable: false,
  writable: false,
  value: 'static'
});

3 Comments

he's not using Object.defineProperty(), so how will this clear up his confusion?
Confusion about properties and keys as evident from his expectation in the question and in the comment to the other answer.
It looks like his confusion is about objects that are nested in other objects. He doesn't understand hierarchy. I'm not sure how the documentation of defineProperty() will help with that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.