Detailed explanation of destructuring assignment syntax in Javascript

Detailed explanation of destructuring assignment syntax in Javascript

Preface

The "destructuring assignment syntax" first introduced in ES6 allows values ​​from arrays and objects to be inserted into different variables. Although it may look difficult, it is actually very easy to learn and use.

Destructuring assignment syntax is a JS expression. ES6 allows you to extract values ​​from arrays and objects and assign values ​​to variables according to a certain pattern, which is called destructuring. Through destructuring assignment, properties/values ​​can be taken out of objects/arrays and assigned to other variables.

Before the emergence of ES6 destructuring assignment, when we needed to assign a value to a variable, we could only specify the value directly.

for example:

let a = 1;
let b = 2;
let c = 3;
let d = 4;
let e = 5;

Array destructuring is very simple. All you have to do is declare a variable for each value in the array. You can define fewer variables, instead of indices in the array (i.e. if you only want to process the first few values), skip some indices or even use the REST pattern to put all remaining values ​​in a new array.

const nums = [ 3, 6, 9, 12, 15 ];
const [
 k, // k = 3
 l, // l = 6
 , // Skip a value (12)
 ...n // n = [12, 15]
] = nums;

Object Destructuring

Object destructuring is very similar to array destructuring, the main difference is that you can reference each key in the object by name, thus creating a variable with the same name. You can also deconstruct the keys into new variable names, deconstruct only the required keys, and then use the rest mode to deconstruct the remaining keys into a new object.

const obj = { a: 1, b: 2, c: 3, d: 4 };
const {
 a, // a = 1
 c: d, // d = 3
 ...rest // rest = { b: 2, d: 4 }
} = obj;

Nested Destructuring

Nested objects and arrays can be destructured using the same rules. The difference is that you can destructure a nested key or value directly into a variable without having to store the parent object in the variable itself.

const nested = { a: { b: 1, c: 2 }, d: [1, 2]};
const {
 a: {
  b: f, // f = 1
  ...g // g = { c: 2 }
 },
 ...h // h = { d: [1, 2]}
} = nested;

Advanced Deconstruction

Since arrays behave like objects, you can use destructuring assignment syntax to get a specific value from an array by using the index as the key in the object destructuring assignment. This method can also be used to obtain other attributes of the array (such as the length of the array). Finally, you can also define default values ​​for variables in the destructuring process if the destructuring value is undefined.

const arr = [ 5, 'b', 4, 'd', 'e', ​​'f', 2 ];
const {
 6: x, // x = 2
 0: y, // y = 5
 2: z, // z = 4
 length: count, // count = 7
 name = 'array', // name = 'array' (not present in arr)
 ...restData // restData = { '1': 'b', '3': 'd', '4': 'e', ​​'5': 'f' }
} = arr;

Summarize

This is the end of this article about deconstruction assignment syntax in Javascript. For more relevant JS deconstruction assignment syntax 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:
  • A practical guide to JavaScript destructuring assignment
  • JavaScript destructuring assignment detailed explanation
  • Javascript destructuring assignment details
  • 5 common scenarios and examples of JavaScript destructuring assignment
  • Detailed explanation of JS ES6 variable destructuring assignment
  • An article to help you understand JavaScript destructuring assignment

<<:  Detailed steps for yum configuration of nginx reverse proxy

>>:  Detailed explanation of using pt-heartbeat to monitor MySQL replication delay

Recommend

JavaScript pre-analysis, object details

Table of contents 1. Pre-analysis 1. Variable pre...

How to use multi-core CPU to speed up your Linux commands (GNU Parallel)

Have you ever had the need to compute a very larg...

Can CSS be used like this? The art of whimsical gradients

In the previous article - The charm of one line o...

Docker container from entry to obsession (recommended)

1. What is Docker? Everyone knows about virtual m...

Overview and Introduction to Linux Operating System

Table of contents 1. What is an Operating System ...

Analysis of examples of using anti-shake and throttling in Vue components

Be careful when listening for events that are tri...

How to clear default styles and set common styles in CSS

CSS Clear Default Styles The usual clear default ...

Vue implements countdown function

This article example shares the specific code of ...

js implements a simple countdown

This article example shares the specific code of ...

What are the rules for context in JavaScript functions?

Table of contents 1. Rule 1: Object.Method() 1.1 ...

Solve the problem that Navicat cannot connect to MySQL on the Linux server

At the beginning, I felt sad. The screenshots are...

MySQL 5.6.33 installation and configuration tutorial under Linux

This tutorial shares the installation and configu...

How to install and configure MySQL and change the root password

1. Installation apt-get install mysql-server requ...

Advantages and disadvantages of common MySQL storage engines

Table of contents View all storage engines InnoDB...