[Vuejs]-Some variables doesn' t work on vuejs

0👍

In javascript, variables as pointers to objects. When you assigning to a variable, you are not modifying any objects, merely repointing your variable to a different object. so if you modify the assigned object, original as well will change.

To overcome this, you have to create a new object which is clone of original object.
There are multiple way to do this, one way as you have pointed out in comment:

var newObj = JSON.parse(JSON.stringify(obj))

There are better and efficient ways to do this, like using Object.assign:

var newObj = Object.assign({}, obj)

underscore and lodash also provides their clone methods.

You can see this answer for other solutions.

Leave a comment