Use Destructuring Assignment In Javascript

Tram Ho

Introduce

In this article, I would like to share about Destructuring Assignment – an extremely useful syntax of JavaScript that I almost use it every day, every time I work with JS.
According to the official definition on MDN , “Destructuring Assignment is a Javascript expression that allows to take the value (value) from within (array) or attribute (properties) in objects and assign new variables.”
In the process of developing JS applications, we rarely work with one or two simple data types such as Number, String, etc., but often have to work with multiple data types, especially with data types. “synthetic” data like Object or Array (in JS, the array is also an Object). Destructuring Assignment helps us interact with Object in a scientific way and is much shorter than Es5 or older syntax, making code easy to read as well as increasing development speed and maintainability.

Use

In this section I will show the typical case of Destructuring Assignment when working with JS.

Use Destructuring Assignment to get the value from Object

In the case we get an Object and want to “peel” into separate values, for example

Using Destructuring Assignment :

The two above code will give similar results, obviously using Destructuring Assignment is more concise, and if we want to use variable names other than the name of the attribute inside Object, such as:

With Destructuring Assignment :

For cases where it is necessary to retrieve values ​​from an object within an object , we use the following:

Note that Destructuring Assignment does not limit the number of object classes that overlap.

Memory management in Javascript
The mouse trap in the Javascript Date.setMonth () function

Use Destructuring Assignment to separate values ​​from Array

Similar to Object, Destructuring Assignment helps the “peeling” value from an Array so easier

Using Destructuring Assignment :

It should be noted that Destructuring Assignment allows to retrieve a correct value according to the index in Array and based on the ‘,’ to distinguish the index, for example if you want to get the first and fourth values ​​in the array:

At the same time we can also use Destructuring Assignment for Array inside Array as follows

A bit more complicated, if combining the Destructuring Assignment between Object and Array, it will be as follows
Array inside Object:

Object inside Array:

To avoid confusion, just be aware that for Object we always rely on the Properties and Index for Array.

Use Destructuring Assignment to pass parameters to Function

When function receives object as parameter, for example:

Destructuring Assignment allows to shorten the above code , becoming:

Similar to Array:

summary

In this article I have shared details about Es6 Destructuring Assignment in Javascript , the concept as well as the cases are often applied, hoping the article is useful for readers.

Javascript – Some tips for converting String to Number
Some Javascript console commands are useful
Share the news now

Source : viblo