使用for / in循环打印对象

时间:2014-05-24 19:08:19

标签: javascript

我正在尝试打印出人员列表,所有人和信息都在对象中,我试图弄清楚如何使用for / in循环打印出对象。

谢谢! 这是我的代码。
http://pastebin.com/TyUXF7Tf

1 个答案:

答案 0 :(得分:0)

var friends = {
  "bill" : {
    "firstName" : "Bill" ,
    "lastName" : "Bob" ,
    "number" : "(111) 111-1111" ,
    "address" : ['1111 1 Road','1 City','1 Province','11111']
  } , {
  "steve" : {
    "firstName" : "Steve" ,
    "lastName" : "G. Jones" ,
    "number" : "(123) 456-7891" ,
    "address" : ['1234 Steve Road','Seattle','WA','12345']
  }
}

var listLoop = function(friends) {
  for(var person in friends) {
    console.log("Whole Person: " , person);
    console.log("First Name: " , person.firstName);
    console.log("Last Name: " , person.lastName);
    console.log("Number: " , person.number);
    console.log("Address: ", person.address);
  }
};