简体   繁体   English

使用 es6 将新属性插入 object

[英]Insert new property to object using es6

I would like to add new object inside the array of objects我想在对象数组中添加新的 object

const valueObj = Object.keys(values);
const filterCompetencies = competencies.filter(item =>
   item.competencies.find(i => !i.isComputational),
);

const filterComp = filterCompetencies.map(item => item.competencies);
const flatten = _.flattenDeep(filterComp);

const auditLogObj = flatten.map(item => ({
  employeeId: _.toNumber(currentUserId),
  actionName: `Updated target for ${item.name}`,
  actionDate: dateToday,
  actionBy: `${mentorSignature.firstName} ${mentorSignature.lastName}`,
}));

console.log(auditLogObj);
console.log(valueObj);

I want my object to insert new object it will look something like this我想让我的 object 插入新的 object 它看起来像这样

{
  employeeId: 243,
  id: "target-3-0",
  actionBy: "Sophia Vaughn",
  actionDate: "Nov 14 2020 14:49:29 PM",
  actionName: "Updated target for Mentorship (as mentee)"
},
{
  employeeId: 243,
  id: "target-3-1",
  actionBy: "Sophia Vaughn",
  actionDate: "Nov 14 2020 14:49:29 PM",
  actionName: "Updated target for Continuous Learning"
},
{
  employeeId: 243,
  id: "target-3-2",
  actionBy: "Sophia Vaughn",
  actionDate: "Nov 14 2020 14:49:29 PM",
  actionName: "Updated target for Certifications Passed"
},
...

在此处输入图像描述

The easiest way is to augment your mapper so that for each new object it creates the corresponding id is taken from valueObj by index:最简单的方法是扩充您的映射器,以便为每个新的 object 创建相应的 id 是通过索引从valueObj中获取的:

const auditLogObj = flatten.map((item, i) => ({
  id: values[i],
  employeeId: _.toNumber(currentUserId),
  actionName: `Updated target for ${item.name}`,
  actionDate: dateToday,
  actionBy: `${mentorSignature.firstName} ${mentorSignature.lastName}`,
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM