Let's talk about destructuring in JS ES6

Let's talk about destructuring in JS ES6

Overview

es6 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 first

Before 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:

1

2

3

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:

1

[twenty three]

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.
As you can see, the emergence of destructuring makes it easier for us to get one or more elements at a specified position in the array. This is also an important application of it in the code.

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.
For example, if we define an object obj, and we want to get the value of its name attribute, we can write it like this:

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:
  • JS quickly master ES6 class usage
  • Detailed explanation of JS ES6 variable destructuring assignment
  • Several magical uses of JS ES6 spread operator
  • JS ES6 asynchronous solution
  • Implementation of Javascript Destructuring in ES6
  • Usage and difference between let and const in ES6 specification in JavaScript
  • 24 ES6 methods to solve practical JS development problems (summary)
  • Detailed explanation of the implementation principle of JavaScript ES6 Class
  • Specific use of ES6 Proxy in JavaScript
  • Detailed explanation of JS ES6 coding standards

<<:  Common commands for deploying influxdb and mongo using docker

>>:  Detailed explanation of 7 SSH command usages in Linux that you don’t know

Recommend

Linux uses stty to display and modify terminal line settings

Sttty is a common command for changing and printi...

How to dynamically add modules to Nginx

Written in front Often, after we install Nginx ba...

Docker deploys mysql remote connection to solve 2003 problems

Connecting to MySQL Here I use navicat to connect...

Using the outline-offset property in CSS to implement a plus sign

Assume there is such an initial code: <!DOCTYP...

JavaScript implements select all and unselect all operations

This article shares the specific code for JavaScr...

Let’s take a look at JavaScript precompilation (summary)

JS running trilogy js running code is divided int...

How to set up Spring Boot using Docker layered packaging

The Spring Boot project uses docker containers, j...

Detailed explanation of MySQL user rights verification and management methods

This article uses examples to illustrate how to v...

Example of using MySQL to count the number of different values ​​in a column

Preface The requirement implemented in this artic...

6 solutions for network failure in Docker container

6 solutions for network failure in Docker contain...

javascript realizes 10-second countdown for payment

This article shares the specific code of javascri...

Example of implementing grouping and deduplication in MySQL table join query

Table of contents Business Logic Data table struc...

js dynamically implements table addition and deletion operations

This article example shares the specific code for...

Detailed usage of docker-maven-plugin

Table of contents Docker-Maven-Plugin Maven plugi...