我在加入这两个时遇到问题
let tasks = [];
this.tasks.map(it => {
tasks.push(
{
'prop1': it['prop1'],
'prop2': it['prop2'],
'prop3': it['prop3'],
}
);
});
let projects = {
'title': this.projectForm.value['title'],
'state': this.projectForm.value['state'],
'comment': this.projectForm.value['comment'],
'other':
// here I want to have the whole "tasks" array with "prop1", "prop2", "prop3".
};
如何将整个响应从任务扔到项目->其他? 当我 console.log 任务时,我有很好的响应,但项目只是没有“其他” Prop ,这使得我的表单无效。
表单是使用 Angular FormBuilder 制作的。
(只是让您知道,在这种情况下使用“:”不起作用 - 未分配数组并且在控制台等中完全跳过属性“other”。)
最佳答案
使用点差运算符
这将完成您需要的事情
let otherObject= {...tasks};
let projects= {'other': {...otherObject}, 'title': this.projectForm.value['title'],
'state': this.projectForm.value['state'],
'comment': this.projectForm.value['comment']
}
注意扩展运算符以“...”开头,是 ES6 功能
我将进一步添加一个使用浏览器控制台(chrome)的示例,这应该有助于进一步澄清。
因此,一位伙伴在评论中提出了一个观点,即用数组代替单个对象 以下是您可以引用的内容;)享受吧!!
使用展开运算符添加另一种方法:
关于angular - 如何合并两个 json 对象(一个是另一个中的数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54196191/