How to unbind an array copy in Vue.js

Vaidas

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?

Andrey Etumyan

As Vue.js documentation says:

Under the hood, Vue.js attaches a hidden property __ob__ and recursively converts the object’s enumerable properties into getters and setters to enable dependency collection. Properties with keys that starts with $ or _ are skipped.

You can store your template array with name started from underscore sign:

  data: {
      testArray: [],
      _templateArray: [{ name: "TEST" }]
  },
  ready: function() {
      this.testArray = this.$data._templateArray;
  }

Or you if need it as a Vue.js object:

this.testArray = JSON.parse(JSON.stringify(this.templateArray));

The second case might be slow for big data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to destroy/remove/unbind SnapSVG.js

From Dev

Marionette.js questions about events and how to correctly unbind them

From Dev

How to unbind an keyboard short cut events in angular js

From Dev

How to filter from array elements in Vue JS

From Dev

How to unbind $on in Angular?

From Dev

How to unbind a key binding

From Dev

How to properly unbind services

From Dev

How to unbind mouseenter (or hover)

From Dev

How to unbind keyup event?

From Dev

How to unBind MusicService

From Dev

How to unbind a shortcut in a terminal

From Dev

Does Vue.js have a built in way to add a copy of a persistent object to a repeated array

From Dev

how to copy an array into a bigger array(partial copy)

From Dev

How to return a copy of an array?

From Dev

How to copy an array or arraylist

From Dev

how to copy an array into a jagged array

From Dev

Vue.js: How can I update an array of objects?

From Java

How to remove an item from an array in Vue.js

From Java

Vue.js - How to use an array in function for looping

From Dev

Vue.js - How to use v-for in JSON array?

From Dev

Vue.js - How to use v-for in JSON array?

From Dev

How to check if item already exists in array in vue.js?

From Dev

How to remove the selected data from an array in vue js?

From Dev

How to get value from an array in vue.js

From Dev

How to unbind ALT in Lubuntu (LXDE)?

From Dev

how to bind and unbind event in subGrid

From Dev

How to dynamically add a new object to array of vue (object) data- Vue.js?

From Dev

How to copy a Object<T> to an array

From Dev

How to copy entire array in constructor