Javascript destructuring assignment details

Javascript destructuring assignment details

1. Array deconstruction

let [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 Deconstruction

The 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 undefined . For example:

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 deconstruction

The 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 exchange

let 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 Javascript deconstruction assignment. For more relevant Javascript deconstruction assignment content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • 5 interesting uses of JS destructuring
  • Parsing destructuring assignments in ES6 version of JavaScript
  • An article to help you understand JavaScript destructuring assignment
  • Example analysis of the usage of JavaScript object destructuring
  • A practical guide to JavaScript destructuring assignment
  • JavaScript destructuring assignment detailed explanation
  • Understanding and application of JavaScript ES6 destructuring operator
  • Summary of common usage of javascript destructuring assignment under ES6

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

Recommend

Learn MySQL in a simple way

Preface The database has always been my weak poin...

Vue detailed introductory notes

Table of contents 1. Introduction 2. Initial Vue ...

MySQL installation and configuration method graphic tutorial (CentOS7)

1. System environment [root@localhost home]# cat ...

MySQL Series 8 MySQL Server Variables

Tutorial Series MySQL series: Basic concepts of M...

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

Preface The requirement implemented in this artic...

Implementing a simple age calculator based on HTML+JS

Table of contents Preface Demonstration effect HT...

How to view Docker container application logs

docker attach command docker attach [options] 容器w...

Detailed explanation of the implementation of shared modules in Angular projects

Table of contents 1. Shared CommonModule 2. Share...

CentOS7 deploys version 19 of docker (simple, you can follow it)

1. Install dependency packages [root@localhost ~]...

centos7.2 offline installation mysql5.7.18.tar.gz

Because of network isolation, MySQL cannot be ins...

Detailed tutorial on installing mysql under Linux

1. Shut down the mysql service # service mysqld s...

The corresponding attributes and usage of XHTML tags in CSS

When I first started designing web pages using XH...

Detailed tutorial on MySQL installation and configuration

Table of contents Installation-free version of My...

Perfect solution for vertical centering of form elements

Copy code The code is as follows: <!DOCTYPE ht...