Why Practicing While Being Distracted Is Good For Your Music

I have always suspected myself of having ADHD due to my lack of focus. Yes, I get distracted easily. When my friend asked me to find music venue for hire, I said yes already but then I got distracted…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Deep Dive into ES6 Array Destructuring

In this article, We will learn about Destructuring, especially Array Destructuring, in depth with easy examples.

So the first question that comes in mind that “What is Destructuring?”

Let’s have a formal definition for Destructuring.

In simple words, Destructuring means extracting data from arrays and objects and store them into new variables.It means you are breaking the structure and fetching the required data from that broken structure.

We can easily extract the data in which we are interested in.It is done through the use of pattern used by ES6.This pattern mirrors the structure of the data item you’re destructuring, and only those data items which matches the pattern are extracted.

So you all be thinking why to use Destructuring, right?

Well, Suppose when we fetch an object from API, we need to extract data for our work purpose. In older JavaScript style, we extract each element from an object with a line. If there is 70-100 element of an object we have to write 70-100 lines for extraction but ES6 is a big relief. It helps us to extract multiple elements from an object with a single line.

We can destructure arrays, objects, maps, function parameters, iterators and regular expressions also.

Before starting Destructuring, you must have knowledge about basic JavaScript concepts like variables, arrays, template literals etc.

So now Let’s start Destructuring with examples.

Destructuring assignment is simple and powerful when working with arrays.

Let’s assume we have an array and we want to assign its contents into separate variables. Without destructuring, you would need to do something like this:

With array destructuring assignment, it is much easier:

If the number of variables > array length , then remaining elements becomes undefined.

Here, starLord didn’t got any value from array,so it becomes undefined.

If the number of variables <array length , then remaining elements gets left out.

Lets see more examples of array destructuring.

Leaving the first item :

For leaving any element don’t write assign any variable, just put a comma there.

We left the first value from the array by just putting up a comma at that place.

Leaving the first and second item :

We left the first and second value from the array by putting up two commas.

Fetching only the first item :

Assignment separate from declaration :

Variables can be declared and and later on value can be assigned via destructuring. Both can be done separately.

Swapping variables :

Two variables values can be swapped using one destructuring expression.

Capture all remaining items with …rest :

If you want to fetch specific array items, but dump the remaining items in their own array, you can use rest operator (…) :

A SyntaxError will be thrown if a comma is used after rest element :

If the (…) operator is written on the right-hand side in destructuring then it is a Spread Syntax. It takes all the other elements in the array which have no variable mapped to them and then maps it to the rest variable.

Default values :

You can also assign default values to variables if you think any value unpacked from array is undefined.

Before providing the default values we are getting starLord as undefined but after providing the default values if any variable becomes undefined then will get into play.

Default values can also refer to other variables including the one in the same array literal.

In case 1, We haven’t provided any values in array so the default values will be assigned to both variables.

In case 2, We have provided only one value so that value goes to spiderMan variable, so spiderMan variable value changes to Tom, and the second variable gets the default value which is spiderMan which is equal to “Tom”.

In case 3, We provided 2 values in array so both the variables gets their values from array respectively.

Function returning array :

A function that return an array, that array can also be destructured.

Here we are capturing only the first element. You can capture as many elements according to your need.

Nested Arrays :

Make sure array pattern on the left of the assignment matches the array structure on the right.

Multiple Array Destructuring :

You can destructure an array more than once.

Here, firstly mcuPhase4 is destructured into tomHolland = “Peter Parker” and otherMembers = ["Stephen Strange", "Wanda Maximoff", "Peter Quill"]. Again spiderMan is matched with tomHolland which is equal to Peter Parker, and drStrange is matched with otherMembers which is an array, so first element from that array is fetched which is Stephen Strange.

I think I have covered everything on Array Destructuring. Still if you have any doubts or any suggestion, please let me know in the comment section.

Keep Learning, Happy Coding.

Add a comment

Related posts:

My Favorite Stories

I began reading at a very early age and the teachers said I read above the level of other kids the same age. Mom had to watch me closely because she knew I had the same voracious appetite that she…

How developers found themselves in the vanguard of labor activism

The pandemic has impacted our lives negatively in so many ways, particularly before vaccines and we were all in lockdown, and one area in particular stands out: our rights as employees. During the…

Why are we scared of freelancers?

When I tell people that I make money writing, there’s a variety of responses. Some are shocked, and enthusiastic. Some seem to think I’ll be the next JK Rowling. This isn’t to attack anyone who…