this question has answer here:
printperson
takes object argument, loop iterates trough properties , suposed print them out.
as can see i've logged both person
, property
variables , working fine. person
identified object, , property
string correct property name, indicating loop working fine. when comes logging person.property
, return value undefined
.
what missing?
var bob = { firstname: "bob", lastname: "jones", phonenumber: "(650) 777-7777", email: "bob.jones@example.com" }; var mary = { firstname: "mary", lastname: "johnson", phonenumber: "(650) 888-8888", email: "mary.johnson@example.com" }; var contacts = [bob, mary]; function printperson(person) { (property in person){ console.log(person); console.log(property); console.log(person.property); } } function list(){ (var i=0;i<contacts.length;i++){ printperson(contacts[i]); } } list();
you need use bracket notation when using variables keys:
console.log(person[property]);
here relevant docs: mdn on objects properties