我想使用javascript spread
运算符,我希望使用spread运算符合并两个不同对象的结果,但它无法正常工作。
例如,每次,对于新请求,新数据即将到来,我想将其附加到单个对象。为了更好地考虑,请考虑简单的场景:
const obj1 = {
WellSchema: {
well: [
{wellName: 'check1', uwi: '9000000123'},
{wellName: 'check2', uwi: '8000012432'}
]
}
}
const obj2 = {
WellSchema: {
well: [
{wellName: 'check3', uwi: '2113120002'},
{wellName: 'check4', uwi: '5679001211'},
{wellName: 'check5', uwi: '0987652221'},
{wellName: 'check6', uwi: '5345676542'}
]
}
}
const mutateIt = { ...obj1, ...obj2 }
console.log(mutateIt)
我得到的结果如下:
{
WellSchema: {
well: [
{wellName: 'check3', uwi: '2113120002'},
{wellName: 'check4', uwi: '5679001211'},
{wellName: 'check5', uwi: '0987652221'},
{wellName: 'check6', uwi: '5345676542'}
]
}
}
无论如何,预期结果应为:
{
WellSchema: {
well: [
{wellName: 'check1', uwi: '9000000123'},
{wellName: 'check2', uwi: '8000012432'}
{wellName: 'check3', uwi: '2113120002'},
{wellName: 'check4', uwi: '5679001211'},
{wellName: 'check5', uwi: '0987652221'},
{wellName: 'check6', uwi: '5345676542'}
]
}
}
你能说一下,这个案子有什么问题吗?感谢