数组中的对象,我如何访问它们?

时间:2015-03-21 11:50:49

标签: javascript arrays object

    var people = [
  { firstname : 'n1', lastname: 'ln1', age: 32, hasSmartphone: false }, 
  { firstname : 'n2', lastname: 'ln2', age: 40, hasSmartphone: true },
  { firstname : 'n3', lastname: 'ln3', age: 81, hasSmartphone: true },
  { firstname : 'n4', lastname: 'ln4', age: 40, hasSmartphone: false } 
                 ];

如何访问Array中的Objects。我需要访问firstName值('n1'...'n4')和hasSmartphone值(true或false)?感谢。

2 个答案:

答案 0 :(得分:2)

for (var i=0;i<people.length;i++) {
  var person = people[i];
  var firstName=person.firstname;
  var sm = person.hasSmartphone;
  ...
}

答案 1 :(得分:0)

访问的例子。

第一个firstName

people[0].firstName < the firstName property of this first object
      ^^
  first object
  in your array

或第三个对象的hasSmartphone

people[2].hasSmartphone < the hasSmartphone property of this third object
      ^^
  third object
  in your array