I'm doing codecademy right now and I'm having an issue with one lesson involving for-in loops. The requirement for thi exercise is to log "bill" and "steve to the console. However, I'm getting the entire array except those two strings logged instead. What's going on here?
var friends = {
bill: {firstName: "Bill",
lastName: "Ferny",
number: "452-556-5412",
address: ['One Bree Hill', 'Bree', 'Middle Earth', '00000']
},
steve: {firstName:"Steve",
lastName:"Rogers",
number:"805-223-5568",
address: ['1500 Settlers Court', 'Paso Robles', 'CA', '93446']
}
};
var list = function (friends) {
for (var p in friends) {
console.log(friends[p]);
}
};
list(friends);
In a for-in
loop, the variable is set to the property names. So if you want to log the property name, use console.log(p)
. friends[p]
is the value that the property name maps to in the object.