1. Array deconstructionlet [a, b, c] = [1,2,3] console.log(a, b, c) // 1 2 3 In addition to arrays, any iterable object can be destructured, such as strings let [first, second] = "he" console.log(first, second) // he 2. Object DeconstructionThe right side of the assignment is the object, and the left side is the variable name separated by commas enclosed in curly braces. let {a, b, c} = {a:1, b:2, c:3} console.log(a,b,c) // 1 2 3 The variable name on the left side needs to be the same as the property name in the object. If they do not match, the variable name on the left side will be assigned to let {a,b, d} = {a:1, b:2, c:3} console.log(a,b,d) // 1 2 undefined If the variable name is different from the attribute name, you can assign the attribute name to the variable name using a colon separator. For example: let {a,b, c:d} = {a:1, b:2, c:3} console.log(a,b,d) // 1 2 3 3. Incomplete deconstructionThe number of variables on the left side of the destructuring assignment may not be equal to the number of elements in the array on the right side (1) Extra variables on the left side will be set to undefined. let [a, b, c] = [1, 2] console.log(a, b, c) // 1 2 undefined (2) Extra values on the right will be ignored directly let [a, b, c] = [1, 2, 3, 4] console.log(a, b, c) // 1 2 3 (3) You can use commas on the left to skip certain values let [a, , c] = [1, 2, 3, 4] console.log(a, c) // 1 3 (4) The extra values on the right side can be collected into a variable by... let [a, b, ...c] = [1, 2, 3, 4] console.log(a, b, c) // 1 2 [3, 4] 4. Use destructuring assignment to implement variable exchangelet a = 1, b = 2; [a, b] = [b, a] console.log(a) //2 console.log(b) //1 This is the end of this article about the details of You may also be interested in:
|
<<: How to avoid garbled characters when importing external files (js/vbs/css)
>>: The Chinese garbled characters in HTML files and the display problems in browsers
Table of contents Understanding SQL Understanding...
1 Implementation Principle This is done using the...
In actual development or production environments,...
In MySQL, most indexes (such as PRIMARY KEY, UNIQ...
Below is the code that Shiji Tiancheng uses to ca...
Table of contents 1. Get the first link first 2. ...
Table of contents 1. How to locate and optimize s...
I wrote some Qt interface programs, but found it ...
System environment: Redis version: 6.0.8 Docker v...
Table of Contents Introduction Synchronous Asynch...
I recently came into contact with MySQL. Yesterda...
Log in to your account export DOCKER_REGISTRY=reg...
Table of contents From father to son From son to ...
Disadvantages of Tables 1. Table takes up more byt...
Effect: Code: <template> <div class=&quo...