我有一个与此类似的JavaScript对象数组
var employees = [
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName": "Jones"}
];
如何在不使用循环的情况下从此对象获取名字数组?
employees.firstName
是否可以在不使用employees [0]的情况下工作并获取名字数组列表?
答案 0 :(得分:3)
var firstNames = employees.map(function(emp){return emp.firstName});
答案 1 :(得分:0)
如果你有underscore.js
,你可以这样做:
var firstNames = _.map(employees, _.property('firstName'));
否则,您可以使用ES5 map
:
var firstNames = employees.map(function(employee){return employee.firstName;});