Deconstruction Assignment
Starting with ES6, JavaScript introduces an understanding of construct assignments, which can be assigned to a set of variables at the same time.
What is a deconstructed assignment? Let's take a look at the traditional way of assigning the elements of an array to several variables individually:
var array = [‘hello‘, ‘JavaScript‘, ‘ES6‘];var x = array[0];var y = array[1];var z = array[2];
Now, in ES6, you can assign values directly to multiple variables using a deconstructed assignment:
Note that when an array element is deconstructed and assigned, multiple variables are [...]
enclosed.
If the array itself is nested, you can also use the following form to deconstruct the assignment, noting that the nesting level and position should be consistent:
let [x, [y, z]] = [‘hello‘, [‘JavaScript‘, ‘ES6‘]];x; // ‘hello‘y; // ‘JavaScript‘z; // ‘ES6‘
Deconstruction assignments can also omit certain elements:
let [, , z] = [‘hello‘, ‘JavaScript‘, ‘ES6‘]; // 忽略前两个元素,只对z赋值第三个元素z; // ‘ES6‘
If you need to remove several properties from an object, you can also use a deconstructed assignment to quickly get the specified properties of an object:
JavaScript------deconstructed Assignment