Skip to main content

Destructuring in Javascript

In JavaScript, destructuring is a feature that allows you to extract data from arrays or objects and assign them to variables. It is a shorthand way of creating multiple variables from an array or object's properties.

Here is an example of destructuring an array:

let numbers = [1, 2, 3, 4, 5];

let [one, two, three, four, five] = numbers;

console.log(one); // 1

console.log(two); // 2

console.log(three); // 3

console.log(four); // 4

console.log(five); // 5

In this above example, the variables one, two, three, four, and five are assigned the corresponding values from the numbers array.

Here is an example of destructuring an object:

let person = {

  name: "John Doe",

  age: 30,

  address: {

    street: "123 Main St",

    city: "Anytown",

    state: "Anystate",

    zip: "12345"

  }

};

let {name, age, address} = person;

console.log(name); // "John Doe"

console.log(age); // 30

console.log(address); // { street: "123 Main St", city: "Anytown", state: "Anystate", zip: "12345" }


In this above example, the variables name, age, and address are assigned the corresponding values from the person object.

You can also use destructuring to assign new variable names to the properties being destructured, like so:

let {name: fullName, age: ageOfPerson, address: fullAddress } = person;

console.log(fullName); // "John Doe"

console.log(ageOfPerson); // 30

console.log(fullAddress); // { street: "123 Main St", city: "Anytown", state: "Anystate", zip: "12345" }

 

Also, you can use destructuring to access nested properties, like so:

let {address: { city, state, zip } } = person;

console.log(city); // "Anytown"

console.log(state); // "Anystate"

console.log(zip); // "12345"

 

Destructuring can also be used to extract data from function arguments:

function getPerson({ name, age }) {

  console.log(name);

  console.log(age);

}

getPerson(person); // "John Doe", 30


Rest Property

The Rest properly which is indicated by ...rest notation is a very unique and useful way to quickly assign remaining properties of the array or object to a completely new object or array.

For example:

const { one, ...numbers} = {a:10, b:20, c:30}

console.log(...numbers}//b:20, c:30

Destructuring is a convenient and powerful feature of JavaScript that can help you write more concise and readable code.

Comments

Popular posts from this blog

API Documentation Overview

 In this blog, we will explore an example of API documentation for one example function findNeedles(). findNeedles() Overview The purpose of the findNeedles() API takes two string parameters i.e. needles array and haystack and finds the total number of occurrences of each element in the needles array that are present in the haystack string. This function searches for only five words or less from the needles array and logs an error if more words are available in the needles array. The comparison is done by first splitting the haystack string using literals like backspace, tab space, single quote, and double quote etc into single elements and then looping over the needles array to find occurrences of these elements. Parameters This function takes following parameters: Name Type Description haystack String A string of any length needles String Array An array of words     Sample Code publi

Understanding Intents in Dialogflow

Dialogflow is a highly robust and dynamic natural language understanding platform that allows you to quickly build highly scalable conversational chatbots by leveraging the power of Artificial Intelligence (AI). You can easily create highly customizable chatbots for variety of channels like web applications, mobile applications, Facebook page, social media apps and much more. It provides seamless integration with variety of channels like Facebook, Viber, Telegram, Slack and much more. But in order to start working on building a chatbot yourself, lets connect the dots to get a clear picture of entire chatbot development process in Dialogflow. To create a chatbot, let us understand some of the nuances of the Dialogflow. What are Intents? Intents help Dialogflow ascertain the real intent or motive of the user messages. You can provide a set of certain training phrases that can help Dialogflow get trained to recognize similar such phrases and trigger a specific intent whenever such

Using Entities in Dialogflow to Extract Vital Information

Having learned about Intents in our previous blog , in this blog we will shed light on another interesting topic of Entities in Dialogflow . Entities provide an efficient way to capture and extract crucial information like name, country, location and any custom information from user inputs. Entities can be mainly categorized into three types: 1.  System Entities : These are build-in entities provided by Dialogflow through which you can identify and extract most common type of data like name, country, date, email address etc. 2. Custom Entities : These are user-defined entities that are useful to extract custom data from the user inputs. For example, you can define a Fruit entity type that can identify and map different fruit types like Mango, Apple etc.        3.  Session Entities : As the name suggests, these entities remain active only for the session for which they have been created. They provide additional capability through which we can update custom entity types with new