Javascript 组合数组中的属性

Javascript 组合数组中的属性,javascript,Javascript,是否有一种方法可以将名为fullName的属性添加到此数组中的项,该属性组合了firstname和lastname let employeeNames = [ { firstName: "Collin", lastName: "Sexton" }, { firstName: "Darius", lastName: "Garland" } ] 预期结果: employeeNames = [ { firstName:

是否有一种方法可以将名为
fullName
的属性添加到此数组中的项,该属性组合了
firstname
lastname

let employeeNames = [ { firstName: "Collin", lastName: "Sexton" }, { firstName: "Darius", lastName: "Garland" } ]
预期结果:

employeeNames = [ { firstName: "Collin", lastName: "Sexton", fullName: "Collin Sexton" }, { firstName: "Darius", lastName: "Garland", fullName: "Darius Garland" } ]

使用
数组#map
迭代员工姓名,并生成全名

让employeeNames=[{firstName:“Collin”,lastName:“Sexton”},{firstName:“Darius”,lastName:“Garland”}],
output=employeeNames.map(o=>({…o,全名:`${o.firstName}${o.lastName}`}))
控制台日志(输出)

.as console wrapper{max height:100%!important;top:0;}
您可以通过分解对象结构并组合值,将名字和姓氏字段映射到新字段

让员工姓名=[
{姓:“科林”,姓:“塞克斯顿”},
{姓:“大流士”,姓:“加兰”}
];
让结果=employeeNames.map(({
名字,
姓,
休息
}) => ({
休息
名字,
姓,
全名:`${firstName}${lastName}`
}));
控制台日志(结果)

作为控制台包装{top:0;max height:100%!important;}
是的,这是可能的。使用
map
employeeNames.map(k=>({…k,fullName:
${k.firstName}${k.lastName}
}))我怀疑关闭的原因是您没有包含任何代码来显示到目前为止在解决此问题时所做的尝试。如果你更新了你的尝试,而失败的是什么,问题可能会被重新打开。
let employeeNames = [ { firstName: "Collin", lastName: "Sexton" }, { firstName: "Darius", lastName: "Garland" } ]

employeeNames = employeeNames.map(employeeName => ({...employeeName, fullName: employeeName.firstName + " " + employeeName.lastName}))

console.log(employeeNames);