Modify the array length in vue

In vue, the modified array length can be divided into two cases

Where a: the length of the array to 0

Case 2: the length of the array to the other number greater than 0

For a case, the length of the array to 0, the array is empty. Method vue emptied in two arrays, as follows:

  the this .arr.splice (0)   // two empty array method vue 
  the this .arr = [];      // two empty array method vue

 

Case 2, for example

arr: [, 2, 3] // array that there are three elements
  Methods: { 
    changeLength () { 
      // set to change the length of the array 
      var the newLength 10 = ;
       // length of the new array - the original length of the array 
      var addLength = the newLength - the this .arr.length;
       var newArr = []; 
      newArr. length = addLength;
       // put a combined array to the original array 
      the this .arr = the this .arr.concat (newArr) 
       the console.log ( the this .arr.length); 
       the console.log ( the this .arr); 
    } 
  },
  < Button @click = "changeLength ()" > changing array length </ Button >

effect:

 

Guess you like

Origin www.cnblogs.com/luguankun/p/11756438.html