Overviewes6 adds a new way to get specified elements from an array or object, which is what we are going to talk about today: destructuring. Let's talk about array deconstruction firstBefore destructuring, we usually get the specified element in the array based on the index: const arr = [1, 2, 3]; const a = arr[1]; After deconstruction, we can use the following method to quickly get an element in the array: const arr = [1, 2, 3]; const [a, b, c] = arr; console.log(a); console.log(b); console.log(c); This prints out the values of a, b, and c respectively:
If we only want to get the first two elements, we can write: const arr = [1, 2, 3]; const [a, b] = arr; console.log(a); console.log(b); We can also combine the spread operator to get multiple elements specified in the array, for example: const arr = [1, 2, 3]; const [a, ...brr] = arr; console.log(a); console.log(brr); In this way, brr is an array consisting of elements other than 1, and the printed values of a and brr are:
What if we only want to get a certain element in the array? For example, if I only want to get 2 in the array, how should I write it? const arr = [1, 2, 3]; const [, a] = arr; console.log(a); Above, we use a comma as a placeholder to ensure that our deconstruction is consistent with the position of the array itself to obtain an element at a specific position. After talking about array deconstruction, let's talk about Deconstruction of objects Unlike array destructuring, object destructuring is matched based on property names. Since objects do not have an order like array subscripts, they cannot be extracted using subscripts. const obj = { name: 'wudixiaodoujie', age: 18 }; const { name } = obj; console.log(name); wudixiaodoujie const age = 0; const { age: perAge } = obj; console.log(perAge); 18 Object deconstruction is widely used. For example, if we need to frequently call a property or method of an object, we can assign it to a variable through deconstruction. Calling it through a variable can reduce the amount of code to a certain extent. The above is the detailed content of deconstruction in JS ES6. For more information about deconstruction in JS ES6, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Common commands for deploying influxdb and mongo using docker
>>: Detailed explanation of 7 SSH command usages in Linux that you don’t know
Sttty is a common command for changing and printi...
Written in front Often, after we install Nginx ba...
Connecting to MySQL Here I use navicat to connect...
Assume there is such an initial code: <!DOCTYP...
This article shares the specific code for JavaScr...
JS running trilogy js running code is divided int...
The Spring Boot project uses docker containers, j...
This article uses examples to illustrate how to v...
Preface The requirement implemented in this artic...
Table of contents UI Design Echarts example effec...
6 solutions for network failure in Docker contain...
This article shares the specific code of javascri...
Table of contents Business Logic Data table struc...
This article example shares the specific code for...
Table of contents Docker-Maven-Plugin Maven plugi...