gpt4 book ai didi

angular - 如何将两个对象数据合并为一个

转载 作者:行者123 更新时间:2023-12-05 08:38:35 32 4
gpt4 key购买 nike

我需要将两个数据合并为一个。我的示例数据是这样的..

第一条数据

{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", …}

合并成一个应该用什么方法

最佳答案

您可以在两者上使用扩展运算符:

const firstData = {Address: "Test", City: "test", State: "Test"};
const secondData = {UserId:"John", Name: "John"};

const merged = { ...firstData, ...secondData };

console.log(merged)

有用的引用:

关于angular - 如何将两个对象数据合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62314088/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com