gpt4 book ai didi

javascript - 在不分区数组的情况下连接两个对象

转载 作者:行者123 更新时间:2023-11-30 09:25:29 26 4
gpt4 key购买 nike

我不确定如何执行此操作,因此将不胜感激。我有两个对象要合并为一个对象。我使用传播运算符来做到这一点:

newObj = {...obj1, ...obj2};

例如,这给了我这个:

{
[
obj1A{
"item": "stuff",
"item": "stuff"
},
obj1B{
"item": "stuff",
"item": "stuff"
}
],
[
obj2A{
"item": "stuff",
"item": "stuff"
},
obj2B{
"item": "stuff",
"item": "stuff"
}
]
}

但我想要的是:

 {
[
obj1A{
"item": "stuff",
"item": "stuff"
},
obj1B{
"item": "stuff",
"item": "stuff"
},
obj2A{
"item": "stuff",
"item": "stuff"
},
obj2B{
"item": "stuff",
"item": "stuff"
}
]
}

有人知道怎么做吗?

最佳答案

有了适当的唯一名称,您可以使用 Object.assign并创建一个新对象。

var object1 = { obj1A: { item1: "stuff", item2: "stuff" }, obj1B: { item1: "stuff", item2: "stuff" } },
object2 = { obj2A: { item1: "stuff", item2: "stuff" }, obj2B: { item1: "stuff", item2: "stuff" } },
combined = Object.assign({}, object1, object2);

console.log(combined);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 在不分区数组的情况下连接两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49284461/

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