Array assignment deconstruction

A plurality of variables can be defined at the same time, the value derived from the existing array

// 1 The number of entries defined variable array ===

let [a, b, c] = [1,2,3]; // equivalent to let a = 1, b = 2, c = 3;  
// 2. Defined variables> several arrays
let [a, b, c] = [1,2]; // a = 1, b = 2, c = undefined defined variable has no value is declared only undefined value is undefined
// 3. defined variable <number of items in the array
let [a, b] = [1,2,3]; // a = 1, b = 2 extra variable value is not received, nothing
 
- the right side is not directly value is an array. Left array directly to get the right values, values ​​corresponding to the format can get
  - Although the variable on the left side, but we still take the time to separate the array variable
the let [A, [B]] = [. 1, [2 ]]; 
the console.log (A, B); // corresponds to a = 1, b = 2

- omitted assignment 

the let [A ,,, B] = [1,2,3,4,5 ]; 
the console.log (A, B); // A first array to get the right 1, b to get the right array the fourth 4. Without intermediate and final assignment does not require an assignment can be omitted 
              // may obtain such values of a and b, var a = ary [0]   

- a left array of a plurality of variable values ​​may acquire the right (not fixed)

the let [, ... A ,, B] = [1,2,3,4,5 ]; 
    the console.log (A, B); // first space corresponding to a right 1, a 2 corresponding to the third vacancies corresponding to 3, ... b value of the remaining 4,5 
                      // a = 2, B = [4,5]

- can be left to a default value, or when the right is not left undefined value corresponding to the left with the defaults

// determines whether there is a location === value, a default value becomes effective when the current right array like undefined or no value, or the default value will not take effect with the value of the right side of the array 
    let [a = 0, b = 1, 2 = C] = [. 1 , undefined]; 
    the console.log (A, B, C); // A =. 1, with the default value. 1 = B, c = 2 with default values

 



Guess you like

Origin www.cnblogs.com/zlsqd/p/11332871.html