6. list 'em all help


#1


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


ok so the code gets accepted, but i dont understand it. also, it prints the names twice. basically, sadly i dont understand anything again. what does a for/in loop actually do? it says how to use it but i dont really understand what purpose its serving. i also dont understand what a key is. i, as usual, have mostly just been going into tese forums, reading other peoples code to try and understand it. i copied/pasted someone elses code to fiddle with and try and work out what it did. this is what i got. lots if people seemed to do very different things though, but this is the only one that worked for me. it seems like just, every variable or 'key' has just been labelled friends. i have no idea what this means or why it works


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "8932900923",
        address: ['One Microsoft Way', 'Redmond', 'WA', '98052']
    },
    steve: {
        firstName: "Steve",
        lastName: "King",
        number: "666",
        address: ['123 Fake Street', 'Yemen Road', 'Yemen']
    }
};

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

if only a day would come where i just get the lesson and dont have to ask for help


#2

The for/in loop cycles through all the keys (values) inside the 'friends' object so its something like:
var friends = {
key1: value,
key2: value,
keyN:value
};
the loop wil go in order hrough the keys: key1, key2, key3...keyN

in the for/in loop the first 'friends' word is just a placeholder and it can be anything you want, but for the console.log it must be the same word.
Example 1:
for(var key in friends) {
console.log(key);
}
Example 2:
for(var anything in friends) {
console.log(anything);
}

finally it prints out the names twices because you execute the function at the end again by calling
list(friends);
deleting this line will make it print the names once.

I hope it helps! :slight_smile:


#3

ok i think i get that some more, thanks! so, if i wanted to log some other data to the console, like number or something, how would i do that?


#4

the way i understand it its the since you already console.log(friends);

it prints it right ? so thats 1 time

if you call the function list again in the end , you print it again!

Like if you console.log x2 so since you re already running it once theres no need for twice!


#6

Hi @oonmw,

If you wanted to print out additional properties to the console, in this case, a number or email property, you could modify the list function just slightly. In this example, I've renamed the function, but it is basically the same code you wrote.

var listDetails = function(number) {
  for(var key in friends) {
    console.log (friends[key].firstName + ": " + friends[key].number);
    } 
   }

listDetails();

Output:

Bill: 8932900923
Steve: 666

There's probably other ways to do this that are more elegant, but it's a start. :slight_smile:


6. List'em all: for/in loops
#7

Where do they cover for/in loops?


#8

Thanks that helped me too!!!! :confused: :sweat_smile: