Angular 如何将两个对象数据合并为一个
我需要将两个数据合并为一个。我的示例数据如下 第一个数据Angular 如何将两个对象数据合并为一个,angular,typescript,Angular,Typescript,我需要将两个数据合并为一个。我的示例数据如下 第一个数据 {Address: "Test", City: "test", State: "Test"} {UserId:"John", Name: "John"} 第二数据 {Address: "Test", City: "test", State: "Test"} {UserId:"John", Name: "John"} 当我使用这个方法时,const a=[].concat(this.firstData,this.secondData
{Address: "Test", City: "test", State: "Test"}
{UserId:"John", Name: "John"}
第二数据
{Address: "Test", City: "test", State: "Test"}
{UserId:"John", Name: "John"}
当我使用这个方法时,const a=[].concat(this.firstData,this.secondData)代码>它在数组中变成一个,而不是组合成一个,如下面的示例:
(2)[{…}, {…}]
[0]{Address: "Test", City: "test", State: "Test", …}
[1]{UserId:"John", Name: "John", …}
我应该使用什么方法合并为一个您可以在以下两种情况下使用spread运算符:
const firstData={地址:“Test”,城市:“Test”,州:“Test”};
const secondData={UserId:“John”,Name:“John”};
const merged={…firstData,…secondData};
console.log(合并)
您可以使用Object.assign
将一个或多个对象中的所有可枚举属性复制到目标对象中。它返回目标对象
var x = { name: 'Jhon', last: 'Lennon' };
var y = { bday: new Date('1940-10-09') };
var z = Object.assign({}, x, y);
console.log(z);
// Output:
// { name: 'Jhon', last: 'Lennon', bday: 1940-10-09T00:00:00.000Z }
有关对象的详细信息。在此处分配: