Shekhu
Shekhu

Reputation: 2020

object.assign angular 2 is not working correctlly

I am using object.assign to create new copy of an object upon making any change in the new copy the changes are reflected in the original copy .but i don't want this

 let xyz = Object.assign({}, this.model);

property i changed is deep inside the model.

Is there any other alternative of object.assign ;as any changes in xyz reflects in original object .I know Object.assign works other way .but not sure what's wrong in my model

Upvotes: 3

Views: 1876

Answers (1)

Logan H
Logan H

Reputation: 3423

Shouldn't it just be something like this

let xyz = this.model;
xyz.obj1.prop1 = "Change";

The change will be reflected in both objects.

OR

If you want a copy but not the refernce to the original you can go like this.

let xyz = JSON.parse(JSON.stringify(this.model));
xyz.obj1.prop1 = "Change";

This will change ONLY the property in xyz and NOT in this.model

Upvotes: 4

Related Questions