Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1488

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T08:39:09+00:00 2024-11-25T08:39:09+00:00

JavaScript: Destructuring Assignment

  • 62k

Definition

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

MDN Documentation HERE

Let's examine some common use-cases for destructuring.

Object Destructuring

First, let's examine a simple example where we destructure the destructo1 object.

const destructo1 = {   name: 'Bob',   wife: 'Jen',   son: 'Patrick',   daughter: 'Anne',   email: 'bob.fornal@leadingedje.com' };  let { name, wife, son, daughter } = destructo1; name = 'Robert'; console.log(name, wife, son, daughter); console.log(destructo1);  // Robert Jen Patrick Anne // {name: 'Bob', wife: 'Jen', son: 'Patrick', daughter: 'Anne', email: 'bob.fornal@leadingedje.com'} 
Enter fullscreen mode Exit fullscreen mode

Array Destructuring

Now, let's examine array destructuring by taking apart destructo2.

const destructo2 = [1, 2, 3, 4, 5]; let [_, a, b, c] = destructo2;  console.log(a, b, c); console.log(destructo2);  // 2 3 4 // (5) [1, 2, 3, 4, 5] 
Enter fullscreen mode Exit fullscreen mode

Default Values

Default values can be managed with destructuring, as well. Here lets use them with destructo3.

const destructo3 = {   name: 'Dave Hawk',   age: 54 };  const { name = 'Bob Fornal', age, height = 60 } = destructo3; console.log(name, age, height); console.log(destructo3);  // Dave Hawk 54 60 // {name: 'Dave Hawk', age: 54} 
Enter fullscreen mode Exit fullscreen mode

Notice the name and age are retrieved from the object. The default for the name is not used in this example, although the height is used.

Use Cases

Now, let's examine some real ways that destructuring can be used.

Regular Expression Groups

Here, we needed to capture the number and string as separate parts for processing. A Regular Expression is used that provides a very specific array output that can then be leveraged using array destructuring.

const maxSize = '10222mb'; const regex = /(d+)(kb|mb|gb|tb)/i; const destructo4 = regex.exec(maxSize); console.log(destructo4);  // ['10222mb', '10222', 'mb', index: 0, input: '10222mb', groups: undefined]  const [_, sizeString, type] = destructo4; console.log({ sizeString, type });  // {sizeString: '10222', type: 'mb'} 
Enter fullscreen mode Exit fullscreen mode

Note how we now have the variables sizeString and type for processing.

Swapping Variables

Traditionally, we would do something like the following for swapping two variable. In this case, we are swapping title1 and title2.

let title1 = 'ABC'; let title2 = 'DEF';  let temp = title1; title1 = title2; title2 = temp; console.log({ title1, title2 });  // {title1: 'DEF', title2: 'ABC'} 
Enter fullscreen mode Exit fullscreen mode

Note the values of title1 and title2 when console log was called.

With destructuring, the code becomes a lot cleaner.

Essentially, we are making an array on the right side of the equal sign with title2 in the zero-index position and title1 in the one-index position. We then destructure the array, assigning title1 and title2.

let title1 = 'ABC'; let title2 = 'DEF';  [title1, title2] = [title2, title1]; console.log({ title1, title2 });  // {title1: 'DEF', title2: 'ABC'} 
Enter fullscreen mode Exit fullscreen mode

The output is the same as the previous example with a different approach.

Removing Keys from an Object

Removing keys from an object can take different forms. A traditional method is to do the following.

const destructo5 = {   name: 'Bob',   wife: 'Jen',   son: 'Patrick',   daughter: 'Anne',   email: 'bob.fornal@leadingedje.com' };  delete destructo5.email; console.log(destructo5);  // {name: 'Bob', wife: 'Jen', son: 'Patrick', daughter: 'Anne'} 
Enter fullscreen mode Exit fullscreen mode

Using the Spread Operator (...) we can remove a key/value pair without impacting the original object.

const destructo1 = {   name: 'Bob',   wife: 'Jen',   son: 'Patrick',   daughter: 'Anne',   email: 'bob.fornal@leadingedje.com' };  const destructo6 = Object.assign({}, destructo1); const { email, ...destructo7 } = destructo6; console.log(email); console.log(destructo7);  // bob.fornal@leadingedje.com // {name: 'Bob', wife: 'Jen', son: 'Patrick', daughter: 'Anne'} 
Enter fullscreen mode Exit fullscreen mode

Note the new variables, email and destructo7. The email key and value are not included in destructo7.

Summary

In this article we've covered some basic examples and real-life use-cases for destructuring of objects and arrays.

developmentfrontendjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.