Reputation: 495
just looking for a dummy explanation of a couple of things in an exercise I'm doing, I don't fully understand it.
I have created an object with two entries here:
var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(292) 676- 3434",
address: ['One Microsoft way',' Redmond', 'WA', '98052']
}
};
I understand this completely I am just putting it as preface so you understand my search function.
I have two functions that I am hoping someone will kindly explain in dummy terms. The first one:
var list = function(friends) {
for (var key in friends) {
console.log(key);
}
};
I understand somewhat - I managed to get it to work but I don't think I fully understand why it works. It's a function that is looking at the friends object right? with that part function(friends)
then a for loop that loops through the entries in my object and logs the key to the console. Which displays Bill and Steve.
What I don't understand is how for (var key in friends)
knows to loop through and store the objects two elements? Is it that it is just a holding variable for that function and because I am defining function(friends)
it simply looks for the parent elements of the object?
Secondly this function:
var search = function(name) {
for (var key in friends) {
console.log...
}
};
I am trying to log to the console the child elements of the two parent elements in the object. so firstName, lastName, number, address for both bill and steve.
I'm unsure of the format to log those to the console, I had tried console.log(friends[bill]);
but I don't think that is the correct syntax.
Any guidance would be great - think I am kinda close.
Upvotes: 0
Views: 36
Reputation: 27823
Objects in JavaScript are collections of key-value pairs. Your friends
object is a collection with 2 key-value pairs. The 2 keys in the collection are bill
and steve
. Each of the 2 values is an object with 4 key-value pairs: firstName -> Bill, etc.
The first function, list
, is a function that takes any object as parameter and logs all its keys. What object it takes depends on what you call it on. The fact that the parameter is named friends
and is identical to the name of your variable is purely coincidental.
For the second function, you need to nest your for loops. Without a function you would do something like this:
for (var name in friends) {
for (var innerKey in friends[name]) {
console.log(name, innerKey, friends[name][innerKey]);
}
}
With the function you already wrote, list
, you could do something like this:
var showNested = function(object) {
for (var name in object) {
console.log('Information for ', name);
list(object[name];
}
}
// Usage:
showNested(friends);
Upvotes: 0
Reputation: 9225
if the object is JSON then the loop will be over the keys as opposed to say an array which will be the array indexes, try the following
console.log(friends[key]);
Upvotes: 0