Search code examples
javascriptobjectractivejsobject-reference

How to remove reference from object in JavaScript


I have an application in ractive.js, and I need to create copy of some data from my component state. example:

const dateGroups = this.get("dateGroups");
this.set("editedPickups.beforeEdit", dateGroups);

And I need to remove reference from "editedPickups.beforeEdit" which is targeted to "dateGroups" because if I change something in "dateGroups" it changes in "editedPickups.beforeEdit" too. I find solution with JSON.stringify and parse, but this object is big and I don't know who this will be acting. This example below not working too:

const dateGroups = Object.assign({}, this.get("dateGroups"));

Solution

  • The example you've posted won't work because Object.assign will shallow copy the object (nested props will still hold references to the original object). You can use lodash.cloneDeep().