javascript object destruction

Javascript Extracting Variables From Arrays And Objects With Object Destruction

In this post i will show you a very powerful feature in Ecma script 2015 which is the Object Destruction feature and how to extract variables from array and object keys.

 

 

Object Destruction means tear a part an object or array into it’s original items which is the inverse of Construction where you build up some sort of collection using keys and values, so the destruction is the process of extracting an object or array items into separate variables where the name of the variable is something you define and the value is the corresponding on that array or object. This feature already exist in a lot of language such as PHP and Python.

 

Terminology

As you see in the above terminology we assign the right hand side which can be an array or object to the left hand side which also can be and array or variables or object of variables.

 

Destructing Arrays

Let’s imagine we have this array

Now if we want to extract this 4 element array to 4 variables like this:

As you see from the output the values assigned in the order which they defined in the array.

You can also declare the variables first:

 

Using default values

In cases where the array contains just little values than the defined variables on the left side you can assign those variables default values which will be replaced by undefined for example:

 

Ignoring specific values

If you need to ignore specific values in the array you can replace it by empty variable like this:

What if you want to assign the rest of the array to a variable, you can accomplish this using the rest expression like shown below:

But don’t use a trailing comma after the rest expression like this:

 

Destructing Objects

Destruction in objects works in the same fashion as arrays, you specify an object of the right side to be assigned to any object of variables on the left side

As you see above we specified the variables in object expression instead of array expression, you can also declare the variables first:

Note here the parentheses is needed in case of object destruction if you omit them you will get a syntax error.

Using new variable names as object values

You can use new variable names as the object values instead of the object keys when extracting like this:

 

Object destructing default values

Default values works in the same manner as arrays, you pass the object keys in the left side a default value like this example:

As you see above we passed default values to the second and third items in the object so that those values be used in case no actual value exist.

You can use also new naming with the default values as in this example:

 

Function parameter default values

Object destruction can very helpful when setting function parameters default values easily, remember in the old days to set default values for the function you may have done something like this:

But using the object destruction syntax this example can be simplified as shown here:

Note how the object destruction syntax makes it easy to use the default values without going through the nasty checks as in the first function, now you can call the function without parameters , with only one parameter or with the two parameters.

Nested Objects

The power of object destruction comes in handy when you need to capture nested object expressions without having to navigate all the object deep to get a certain value, consider this example from google maps autocomplete api response:

Now if for example you need to access some nested keys like lat or lng you may have to do something like this:

Using object expressions this can simplified like shown below:

As you see we extracted the nested properties with an easy way using the closest parent autocompleteResponse.geometry.location

Using the same methodology we can access other values like this:

 

Share this: