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 7238

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

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T01:57:07+00:00 2024-11-28T01:57:07+00:00

Object and Array Destruction

  • 60k

Introduction

Destruction is a way to extract data (key, or element) from an object or array and assign it to a variable.

Object Destruction

Object Destruction is a way to extract data from an object and assign it to a variable.

Syntax

const obj = { property1: "abc", property2: 123, property3: true };  const { property1, property2, property3 } = obj;  console.log(property1); // abc console.log(property2); // 123 console.log(property3); // true 
Enter fullscreen mode Exit fullscreen mode

In the above syntax, property1, property2, and property3 are the variables that will store the values of the properties of the object. And object is the object from which we want to extract the data.

Example

const person = {     name: "John",     age: 30,     city: "New York", };  const { name, age, city } = person;  console.log(name); // John console.log(age); // 30 console.log(city); // New York 
Enter fullscreen mode Exit fullscreen mode

Plain Object Destruction

const obj = { a: 1, b: 2, c: 3 }; const { a, b, c } = obj;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the object obj and assigned the values of the properties a, b, and c to the variables a, b, and c. The variables a, b, and c are created automatically by JavaScript.

Nested Object

Nested object is an object that is inside another object.

const person = {     name: "John",     age: 30,     city: "New York",     address: {         street: "Main Street",         number: 123,     }, }; 
Enter fullscreen mode Exit fullscreen mode

Method 1:

const obj = {     a: 1,     b: 2,     c: 3,     d: {         e: 4,         f: 5,     }, };  const { a, b, c, d } = obj;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 console.log(d); // { e: 4, f: 5 } 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the object obj and assigned the values of the properties a, b, c, and d to the variables a, b, c, and d. But the value of the property d is an object, so it will be assigned to the variable d as it is.

Method 2:

const obj = {     a: 1,     b: 2,     c: 3,     d: {         e: 4,         f: 5,     }, };  const {     a,     b,     c,     d: { e, f }, } = obj;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 // This will error console.log(d); // ReferenceError: d is not defined // because we have destructured the property `d` and assigned it to the variable `e` and `f` console.log(e); // 4 console.log(f); // 5 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the object obj and assigned the values of the properties a, b, c, and d to the variables a, b, c, and d. But the value of the property d is an object, so we have destructured the object d and assigned the values of the properties e and f to the variables e and f.

Renaming Variables

First of all, why do we need to rename the variables

Suppose we have an object with a property a and we want to extract the value of the property a and assign it to a variable x. But we already have a variable x in our code. So, we can rename the variable x to y and assign the value of the property a to the variable y.

We use the colon : operator to rename the variables. The syntax is variable: newVariableName.

const a = 10; const b = 20; const c = 30;  const obj = { a: 1, b: 2, c: 3 };  // as a, b, and c are already declared, I can't destructured the object and assign the values to a, b, and c // so I have to rename the variables a, b, and c to x, y, and z const { a: x, b: y, c: z } = obj;  console.log(x); // 1 console.log(y); // 2 console.log(z); // 3  console.log(a); // 10 console.log(b); // 20 console.log(c); // 30 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the object obj and assigned the values of the properties a, b, and c to the variables x, y, and z. But we have renamed the variables a, b, and c to x, y, and z using the colon : operator.

Default Values

Default values are used when the property does not exist in the object.

Suppose we have an object with a property a and we want to extract the value of the property a and assign it to a variable x. But we don't know if the property a exists in the object or not. So, we can assign a default value to the variable x and if the property a exists in the object, then the value of the property a will be assigned to the variable x. If the property a doesn't exist in the object, then the default value will be assigned to the variable x.

const obj = { a: 1, b: 2, c: 3 }; const { a, b, c, d = 4 } = obj;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 console.log(d); // 4 
Enter fullscreen mode Exit fullscreen mode

Rest Operator ...

Rest operator is used to extract the remaining properties of the object.

Suppose we have an object with a property a and we want to extract the value of the property a and assign it to a variable x. But we want to extract the remaining properties of the object and assign them to a variable y. So, we can use the rest operator to extract the remaining properties of the object and assign them to a variable y.

const obj = { a: 1, b: 2, c: 3 }; const { a, ...y } = obj;  console.log(a); // 1 console.log(y); // { b: 2, c: 3 } 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the object obj and assigned the value of the property a to the variable a. And we have used the rest operator to extract the remaining properties of the object and assigned them to the variable y.

Array Destruction

Array Destruction is a way to extract data from an array and assign it to a variable.

Syntax:

const [variable1, variable2, variable3] = array; 
Enter fullscreen mode Exit fullscreen mode

Plain Array

const arr = [1, 2, 3]; const [a, b, c] = arr;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the array arr and assigned the values of the elements 1, 2, and 3 to the variables a, b, and c.

Skipping Elements

const arr = [1, 2, 3, 4, 5]; const [a, b, c, d, e] = arr;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3  // we can skip the elements const [a, , c, , e] = arr;  console.log(a); // 1 console.log(c); // 3 console.log(e); // 5 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the array arr and assigned the values of the elements 1, 2, 3, 4, and 5 to the variables a, b, c, d, and e. But we have skipped the elements 2, 4, and 5 by using the comma , operator.

Nested Array

Method 1:

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

In the above example, we have destructured the array arr and assigned the values of the elements 1, 2, 3, and 4 to the variables a, b, c, and d. But the value of the element 4 is an array, so it will be assigned to the variable d as it is.

Method 2:

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

In the above example, we have destructured the array arr and assigned the values of the elements 1, 2, 3, and 4 to the variables a, b, c, and d. But the value of the element 4 is an array, so we have destructured the array 4 and assigned the values of the elements 4 and 5 to the variables d and e.

Using Rest (spread) Operator ...

... (spread operator) is used to extract data from an array and assign it to a variable. It is used to extract the remaining elements of an array.

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

In the above example, we have destructured the array arr and assigned the values of the elements 1, 2, and 3 to the variables a, b, and c. But we have assigned the remaining elements to the variable d using the spread operator ....

Array of Objects

const arr = [     { a: 1, b: 2 },     { c: 3, d: 4 },     { e: 5, f: 6 }, ];  const [{ a, b }, { c, d }, e] = arr;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 console.log(d); // 4 console.log(e); // { e: 5, f: 6 } 
Enter fullscreen mode Exit fullscreen mode

Default Values

We can assign default values to the variables in array destruction. If the element exists in the array, then the value of the element will be assigned to the variable. If the element doesn't exist in the array, then the default value will be assigned to the variable.

const arr = [1, 2, 3]; const [a, b, c, d = 4] = arr;  console.log(a); // 1 console.log(b); // 2 console.log(c); // 3 console.log(d); // 4 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have destructured the array arr and assigned the values of the elements 1, 2, and 3 to the variables a, b, and c. But the array arr does not have the element 4, so we have assigned the default value 4 to the variable d.

Swapping Variables

let a = 1; let b = 2;  [a, b] = [b, a];  console.log(a); // 2 console.log(b); // 1 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have assigned the value of the variable a to the variable b and the value of the variable b to the variable a using array destruction.

Returning Multiple Values from a Function

function add(a, b) {     return [a + b, a - b]; }  const [sum, difference] = add(5, 3);  console.log(sum); // 8  console.log(difference); // 2 
Enter fullscreen mode Exit fullscreen mode

In the above example, we have created a function add that takes two arguments a and b and returns an array containing the sum and difference of the arguments. We have destructured the array returned by the function and assigned the values of the elements to the variables sum and difference.

beginnersjavascriptprogrammingwebdev
  • 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.