Data Extraction With Destructuring in JavaScript

Data Extraction With Destructuring in JavaScript

Data Extraction With Destructuring in JavaScript. Destructuring is a data extraction technique that was added to the Javascript language in ES2015. If you are looking for some way to extract data from objects or arrays, or swap values between variables, saving lines of code, with Destructuring you find an elegant and easy way to do it. To work with Destructuring we must know the way it works since its behavior is different from an Array than with an Object for example. Each of these different ways of functioning is explained below, which will serve as the basis for understanding this feature called Destructuring of Javascript.

Image for post

Destructuring is a data extraction technique that was added to the Javascript language in ES2015. If you are looking for some way to extract data from objects or arrays, or swap values between variables, saving lines of code, with Destructuring you find an elegant and easy way to do it.

To work with Destructuring we must know the way it works since its behavior is different from an Array than with an Object for example.

Each of these different ways of functioning is explained below, which will serve as the basis for understanding this feature called Destructuring of Javascript.

Sequence.

The sequence is a term that matters when talking about Destructuring. Basically, it tells us that the order in which we arrange the variables is important in Arrays but not so much in Objects.

Let’s see an example:

Image for post

In the example above, we have our first case where we are applying Destructuring. We have a String with a date. Then on line 2, we see how in a single line, we have converted our String into an Array and each element of the new Array, we have assigned it to three different variables, one that will contain the day, another that will contain the month and another that will contain the year. Later we proceed to print each variable on the screen and see how each variable corresponds to a value in the sequence, that is, for each variable to keep the value we want, we must consider that it corresponds to the order of our array.

Specific extraction.

Going back to the previous example of our String converted to Array, what if we only want to extract some elements from that Array and not all of them ? for that we can use empty spaces, in which we indicate that this element is not taken into account:

Image for post

We see how only the day and year are printed on the screen since we have left one empty space for the month (line 10), so it will not be taken into account.

objects destructuring arrays javascipt extraction programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Find Ulimit For user on Linux

Explains how to find ulimit values of currently running process or given user account under Linux using the 'ulimit -a' builtin command.

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS

MEAN Stack Tutorial MongoDB ExpressJS AngularJS NodeJS - We are going to build a full stack Todo App using the MEAN (MongoDB, ExpressJS, AngularJS and NodeJS). This is the last part of three-post series tutorial.

Creating RESTful APIs with NodeJS and MongoDB Tutorial

Creating RESTful APIs with NodeJS and MongoDB Tutorial - Welcome to this tutorial about RESTful API using Node.js (Express.js) and MongoDB (mongoose)! We are going to learn how to install and use each component individually and then proceed to create a RESTful API.

systemctl List All Failed Units/Services on Linux

Explains how to use the systemctl command to list all failed units or services on Debian, Ubuntu, CentOS, Arch, Fedora, and other Linux distros.

CentOS Linux 8.2 Released and Here is How to Upgrade it

CentOS Linux 8.2 (2004) released. This release adds corrections for security issues based upon RHEL 8.2 souce and here is how to upgrade it.