数组中的对象,我如何访问它们?
Objects in Array, how can i access to them?
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 }
];
我怎样才能访问数组中的对象。我需要访问 firstName 值 ('n1' ... 'n4') 和 hasSmartphone 值(true 或 false)?谢谢
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
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 }
];
我怎样才能访问数组中的对象。我需要访问 firstName 值 ('n1' ... 'n4') 和 hasSmartphone 值(true 或 false)?谢谢
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