0

I have two objects. First object old object. Second object new object. As a result I want to combine these objects. How do I go the easy way. Example:

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


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

// result 
{
  name: 'change name',
  surname: 'change surname',
  number: '123456'
}
2

2 Answers 2

0

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);

Sign up to request clarification or add additional context in comments.

2 Comments

I was not. I updated the subject.
Update... not fix.
0

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"}

1 Comment

I was not. I updated the subject.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.