5

I have an object coming from my API and when loading a modal I need to 'duplicate' the object to another.

This works:

this.servicesForm.services = this.team.services;

// New object                // API object

The issue now is that I DON'T want the team.services object to be bound to and update when I update the servicesForm.services object.

How do I do that?

1
  • Does the copied object have nested objects? Commented Jul 5, 2017 at 11:50

2 Answers 2

9

Quickly found my answer:

this.servicesForm.services = JSON.parse(JSON.stringify(this.team.services));
Sign up to request clarification or add additional context in comments.

Comments

3

An ES6 solution would be to use Object.assign:

this.servicesForm.services = Object.assign({}, this.team.services); 

Note that this is only a shallow copy, if you need a deep copy you would need to apply this method recursively.

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.