An array of deconstruction

Es6 allows you to extract values from arrays and assign values to variables in their respective positions.

let [a,b,c] = [1.2.3];
console.log(a, b, c);/ / 1, 2, 3
Copy the code

Variables a, b, and c correspond to 1,2,3 respectively

[a, B,c] on the left do not represent arrays, they represent deconstruction, extracting values from arrays

let [a, b, c] = [1.2.3.4]; 
console.log(a, b, c);/ / 1, 2, 3
Copy the code
let [a, b, c] = [1.2]; 
console.log(a, b, c);// 1 2 undefined
Copy the code

Object to deconstruct

let person = { name: "Jenny".age: "18".sex: "girl" };
let { name, age, sex } = person;
console.log(name, age, sex);
Copy the code

On the left side of the {} are variables to which the attributes of the object are assigned;

let person = { name: "Jenny".age: "18".sex: "girl" };
let {name: myName,age: myAge} = person; // myName myAge belongs to an alias
console.log (myName) ; // 'Jenny '
console.log (myAge); / / 18
Copy the code

In {} on the left, the name and ag on the left are only used to correspond to the attributes of person. The real variables are myName and myAge on the right