ecmascript6传播运算符无法按预期运行javascript对象

时间:2018-05-31 08:39:33

标签: javascript ecmascript-6

我想使用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'}
          ]
        }
      }

你能说一下,这个案子有什么问题吗?感谢

0 个答案:

没有答案