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

Objects in Array, how can i access to them?

本文关键字:访问 何访问 对象 数组      更新时间:2023-09-26
    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 } 
                 ];

如何访问数组中的对象。我需要访问名字值('n1' ...'n4'(和智能手机值(真或假(?谢谢。

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

访问示例。

第一个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