This behavior is causing problems in my projects. This is a simplified version of what’s happening. I’d like to learn why this happens, and how I can avoid it. I have vue loaded in the head of my website:
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
And this is the Vue object:
vueObject = new Vue({
el: '#app',
data: {
"array": []
}
});
Now, my issue is this. How do I save a copy of the “array” data item from the Vue Object so I can manipulate it without actually modifying the real array Vue data item? This is what I’m trying to do, but it isn’t working as intended;
var arrayCopy = vueObject.array;
arrayCopy.push("x");
This is causing both arrayCopy and vueObject.array to be set to [“x”]. I only want to add “x” to arrayCopy. How can I do that? Why does this happen?