可以说我有一个对象数组:
var employees=[]
employees[0]={name:"George", age:32, retiredate:"March 12, 2014"}
employees[1]={name:"Edward", age:17, retiredate:"June 2, 2023"}
employees[2]={name:"Christine", age:58, retiredate:"December 20, 2036"}
employees[3]={name:"Sarah", age:62, retiredate:"April 30, 2020"}
Run Code Online (Sandbox Code Playgroud)
是否有一个数组函数,允许我将一个属性作为数组,例如:
namesArray = employees.reduceToProperty('name'); // none existent function i made up!
// should return ["George","Edward","Christine","Sarah"]
Run Code Online (Sandbox Code Playgroud)
我知道如何通过循环获得所需的结果,我只是希望存在一个数组函数或函数组合,可以在一行中完成此操作.
phy*_*lax 12
var names = employees.map(function(i) {
return i.name;
});
Run Code Online (Sandbox Code Playgroud)
names现在是一个包含name对象的-properties 的数组.