I am trying to copy one array to another and use this like the new array without any changes to old one:
<div id="app">
<div class="form-group">
<label>Test input</label>
<input v-model="testArray[0].name" type="text" class="form-control" placeholder="Input">
</div>
<br>
<pre>testArray: {{ testArray[0] | json}}</pre>
<pre>templateArray: {{ templateArray[0] | json }}</pre>
new Vue({
el: '#app',
data: {
testArray: [],
templateArray: [{name: "TEST"},],
},
ready: function() {
this.testArray = this.templateArray.slice(0);
},
});
the issue is that then I am updating new array 'testArray' I also change old array 'templateArray'.
The script in action: https://jsfiddle.net/4po1cpkp/7/
Is there any way to create new array based on array template without directly binding it to template?
As Vue.js documentation says:
You can store your template array with name started from underscore sign:
Or you if need it as a Vue.js object:
The second case might be slow for big data.
I used Vue extend function Vue.util.extend to copy array with un-binding in Vue 2:
You can use
slice()
of array prototype read more in MDN Array.prototype.slice()this.testArray = [].slice.call(this.templateArray)