11 Instagram Accounts To Follow For Post Inspiration

The activity-ready camera brand’s page is as versatile as their cameras. From skydiving, to scuba diving, and surfing to snowboarding, GoPro’s showstopping photos and videos take you into another…

Smartphone

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




Learn about Destructuring in JavaScript

Destructuring is one of the most useful features in ES6, it offers a quick and easy way to extract values from objects and arrays.

In this post, we will look at how to destructure objects and arrays.

Check out this new API tool:

Let’s say we have an object like this:

To access the values, we need to reference the properties.

We use the property name in the obj to access the value.

We can also, assign variables to the property, like this:

Using Destructuring, we can re-write the above to this:

Now, using prp1 will have the value 67.

We can destructure many properties from the obj at once:

prp1 will hold 67 and prp2 will hold "nnamdi":

Note, to destructure a property from an object, it follows this format:

The type of the scope is first declared, whether a let or const or var. Then, we open and close curly brackets. Inside the brackets, we make a colon, on the left of it is the name of the property we want to destructure and on the right is the variable that will hold the value of the property. Destructuring multiple properties in one curly brackets will be done in a comma-separated list.

If we wanted to extract the properties into variables with the same name we would write it like so:

We can still add the colon:

The elements in an array are indexed by numbers.

We know that elements in arrays are referenced by their index number. The first is 0, the second is 2, the last is (array.length -1). These indexes are the properties in an array.

So, this

will look like this

as an object:

See, the properties are numbers and they are referenced by that. That’s why to reference the first value of an array, you will do this:

like in an object when you reference a property:

Just like destructuring an object with its properties name, same is done with arrays just that the properties names are numbers:

Here, we extracted the first element (which has the property name “0”) from the arr array and created a first variable to hold the element.

See, it is the same as an object. The first variable will hold 45.

We can destructure more indexes from the arr array:

The second element in the arr array is 46 and its referenced by number 1. So we add 1 and : then the variable second to hold the value of the property "1" in arr.

We can destructure the length of an array.

Yes, we do it by referencing “length”, this is possible because “length” is a property of an array, and it holds the amount of the elements in an array:

The length of the arr is extracted via “length” and held in the “len” variable.

Without curly braces

We can also destructure arrays like this:

You see, we are no longer using the curly braces and specifying the index number. We are using the array square brackets [].

The elements will be destructed from the arr array. The first and second are variables that will hold the first element and the second element in the arr respectively. The element at index 0 will be assigned to first and the element at index 1 will be assigned to second. the reason is that the first is at index 0 and the second is at index 1.

If we switch their places:

second will hold the element at index 0 in arr and first will hold the element at index 1. So the placing of the variables determines the index it will destructure.

Function parameters can be destructured. This can occur when objects are passed as parameters to functions.

We can use destructuring to extract the prop property from the opts argument in the function.

We know the function argument is expecting an object with prop property. We replaced opts argument with {prop}.

This will extract the prop property from the argument coming in, create a variable prop (of the same name with the property destructured), then assign the extracted prop property to the prop variable.

To extract the prop property to a variable of a different name, we do this :

Because we are destructuring to a variable of different name we had to add : and the variable name, p. p will hold the value of the prop property.

Destructuring is a very exciting and wonderful feature in ES6.

If you have any question regarding this or anything I should add, correct or remove, feel free to comment, email or DM me

Thanks !!!

Thanks for stopping by my little corner of the web. I think you’ll love my email newsletter about programming advice, tutoring, tech, programming and software development. Just sign up below:

Add a comment

Related posts:

About Consciously Unbiased

Consciously Unbiased is a movement to build a world of greater belonging, one story at a time, by amplifying voices that have been muted for too long. We create change inside organizations through…

TELEMEDICINE

The usage of telemedicine has been around for ages. However, in the last few decades, a rising trend has been seen in its consumption. The main reason for this is the advancement of technology…

Stabilizer

Aadishakti Industry was founded and guided by Mr.P.K. Kushwaha and from a modest beginning, has grown to be one of India’s premier and fast growing manufacturers and suppliers of single phase and…