调整对象结果Javascript [重复]

我有两个对象。第一个对象是旧对象。第二个对象是新对象。结果,我想组合这些对象。我如何轻松进行。 例:

// old object
{
  name: 'exapmle name',
  surname: 'example surname',
  number: '123456'
}


// new object
{
  name: 'change name',
  surname: 'change surname',
}

// result 
{
  name: 'change name',
  surname: 'change surname',
  number: '123456'
}
评论
  • qq情侣
    qq情侣 回复

    I think spread syntax can help for this purpose.

    const old = { name: 'exapmle name', surname: 'example surname', number: '123456' };
    const newObj = { name: 'change name', surname: 'change surname', };
    
    const result = { ...old, ...newObj };
    
    console.log(result);
  • 时间淡一切
    时间淡一切 回复

    You can use Object.assign as so:

    oldObj = {
      name: 'exapmle name',
      surname: 'example surname',
      number: '123456'
    }
    
    newObj = {
      name: 'change name',
      surname: 'change surname',
    }
    
    result = Object.assign({}, newObj, oldObj)
    
    result => {name: "exapmle name", surname: "example surname", number: "123456"}