On looping elements of objects & elements of objects within objects


#1


I am stuck at the search function, Part 7 of the Contact List exercise.


So, on the final function "search" I am really unclear as to how the expression in the for loop works. what is it looping through and what is that expression telling the loop to look for?

Because initially I thought that the expression will take the declared variable and use it as a placeholder for each element (key) of the object it's searching through.
Minding that since my objects are in global scope I shouldn't need to enter them into my parameters.
So my thought was if the key's that the for loop is using are objects, I should be able to treat the 'var key' that the loop is looping through as objects and access the current object's keys individually and log them all, and access them for the purpose of comparing the intended name, and the loops current object's firstName key. (That is a lot of hoops, So sorry!!!!)

So what is the "var key in object" expression as the for loop parameter doing, and why do I need to change my function parameters &/or looping expressions to fit that?

Thank you so much


var friends = {};

friends.bill = {
 firstName: "Bill",
 lastName: "Preator",
 age: 42,
 occupation: "Born rich",
 phoneNumber: "(206) 790-4568",
 address: ['78905 Prince Creek Dr.', 'Parker', 'Colorado', 80134]
};

friends.steve = {
 firstName: "Steve",
 lastName: "Bannon",
 age: 31,
 occupation: "Software Designer",
 phoneNumber: "(509) 678-5678",
 address: ['4567 37th Ave SW', 'Seattle', 'Washington', 98126]
};

friends.ramiro = {
 firstName: "Ramiro",
 lastName: "Gonzales",
 age: 21,
 occupation: "professional beggar",
 phoneNumber: "(206) 553-6780",
 address: ['3959 Recreation Ln', 'Naples', 'FLorida', 34116]
};

/***********************************************************/


function myList(someObject){
    for (var key in someObject){ //How does this looping expression work?
        console.log(someObject.key);
    }
}

function search(name, someObject){
    for (var key in friends){ //alright so key is supposed to map to the sub-objects (steve, bill, and ramiro) within the object friends.
    //How exactly does the expression "var key in friends" **ESPECIALLY THE --IN OBJECT-- PART WORK? How is it supposed to sift through all of the different elements within an object?
        if (friends.key.firstName === name){ //<--- see the key!!
            console.log(friends.key.firstName);// this feels wrong. But I am unclear of how it is supposed to work.....
            console.log(friends.key.lastName);
            console.log(friends.key.phoneNumber);
            console.log(friends.key.address);
        }
    }
}

//The issue is that the 

search("Bill"); //Where Ultimately this is supposed to work


#2

Ok, I thought about something that might have some assistance as to what is going wrong.

I think maybe the above isn't working because the loop expression -- var key in object-- works in such a way that if it's declared, and the loop enters an object, let's say a global object; where the global object's keys are themselves objects, the looping expression will then enter the 'local object or sub-object' where that is the object within the global object.

So when I declare that function I don't need to worry about calling the objects key because the loop has already entered the sub-object through the nature of the working of the loop and the expression used.

Now if that is the case, why is the console shooting me a message saying that the keys of each sub-object are undefined when as I understand it they are defined above the functions in the body of the code?


#3

run this code:

var friends = {};

friends.bill = {
 firstName: "Bill",
 lastName: "Preator",
 age: 42,
 occupation: "Born rich",
 phoneNumber: "(206) 790-4568",
 address: ['78905 Prince Creek Dr.', 'Parker', 'Colorado', 80134]
};

friends.steve = {
 firstName: "Steve",
 lastName: "Bannon",
 age: 31,
 occupation: "Software Designer",
 phoneNumber: "(509) 678-5678",
 address: ['4567 37th Ave SW', 'Seattle', 'Washington', 98126]
};

friends.ramiro = {
 firstName: "Ramiro",
 lastName: "Gonzales",
 age: 21,
 occupation: "professional beggar",
 phoneNumber: "(206) 553-6780",
 address: ['3959 Recreation Ln', 'Naples', 'FLorida', 34116]
};
for (var key in friends){
  console.log(key)
}

as you can see, key is a string. If you don't believe me, you can use typeof(key) which will tell you key is a string

so: the for in loop stores the values it assignes to key as string, this is imporant.

when using dot notation, the object we want to access has to be name of object:

friends.bill

bill can't be string:

friends."bill"  // invalid

so, this:

for (var key in friends){
  console.log(friends.key)
}

is also invalid, because key is of type string

we have to use the associative array notation (square brackets) when we want to access on object by string


#4

That is what I figured, So then how would I keep track of which sub-object I am in?
ie: friends.bill is the target in my above code.
So that I would be able to log the contact information of only that one person (certain keys of a specific sub-object).

I have no idea how to allow the loop to keep track of which object the for loop is inside of.


#5

i thought i covered it:

This should have been covered somewhere? Let me go and find it

found it:

https://www.codecademy.com/en/courses/spencer-sandbox/1/4

the string used can of course also be a variable (like the loop iterator)


#6

Oh I understand that I am not able to call keys of an object because within the for loop, it is looping through keys and not objects. So I know I can't do "key.firstName" because within the loop the "key" isn't an object, it's a key to an object.

What I am wondering though, is how can I determine which object (ie: bill) I need to log contact information for, through comparing the first name entered into the function and comparing it to each first name key that is defined within sub-objects?


#7

well, your function for starters should only have one parameter (name, for example, this will be the name we are going to search for

then here:

if (friends.key.firstName === name){

you are pretty close, except, like we discussed, key contains a string value, so associative array notation (square brackets) have to be used


#8

ok, so how are you going to loop through all of the names without going into each object using a for loop where; as I am understanding it, the "key" isn't a sub-object it's a particular key of on of the sub-objects depending on which iteration the for loop is on.


#9

you can use the for in loop, but you will need to change .key to [key] since key contains a string value


#10

I'm sorry, i'm kind of confused. The key would loop through the object friends like this
key = null -> friends -> steve -> firstName, then the second iteration of the loop would goto the second attribute lastName... then once steve is done it would jump back to friends and goto the second attribute of friends which would be bill so...
key = steve.address -> bill.firstName then would loop again bill.lastName just like it did with steve.

so I have no way of picking which attributes out of my friends I want to be displayed in the console. I'm trying to get it so that only specific attributes that I am going to eventually ask the user are supposed to be displayed and which one's to ignore.


#11

then you need a second loop to loop over the properties of bill and steve


#12

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