Learn Javascript Destructuring using Github Copilot Labs

Setup

npm init -y to start your project.
A file name called index.js

I will be using GitHub Copilot Labs to further explain my code base.

To get GitHub Copilot Labs install the GitHub copilot extension or join the waiting list.


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi

Setup

npm init -y to start your project.
A file name called index.js
Image description

I will be using GitHub Copilot Labs to further explain my code base.

To get GitHub Copilot Labs install the GitHub copilot extension or join the waiting list.

Image description

Usage

Highlight the code snippets and click ask Copilot
Image description

Object Destructuring

Let's write an object of student with a name and age.

The old way to get properties from object is:

const student = {
  name: 'John',
  age: 30,
};

console.log(student.name);
console.log(student.age);

Image description

Explanation: The code is written in ES6 syntax.

Here is the explanation for the code above:

  1. The student object has name and age as its properties.
  2. student.name refers to the property name in the student object.
  3. student.age refers to the property age in the student object

Destructuring

const student = {
  name: 'John',
  age: 30,
};

const { name, age } = student; 
console.log(name);
console.log(age);

Note: Left hand side holds the properties in curly brackets while the Right hand holds the object
Result:

Image description

Default assignment on values
const student = {
  age: 30,
};

const { student_name = 'James', age } = student;
console.log(student_name);
console.log(age);
Explanation:

javascript
Here is the explanation for the code above:

  1. const student = {};
  2. const { student_name = 'James', age } = student; The above code will assign the student_name and age variables with the value of the student object.

If the student_name key does not exist in the student object, then the student_name variable will be assigned the value of James.

If the age key does not exist in the student object, then the age variable will be assigned the value of 30.

Destructuring and Extracting values in Nested object
const person = {
  name: 'John',
  age: 30,
  pet: {
    height: 0.4,
    breed: 'Caucasian',
  },
};

const {
  pet: { breed: doggy },
} = person;

console.log(doggy);
Explanation
  1. We're destructuring the person object and pulling out the pet object as pet.
  2. We're saying that we want the pet object's breed property to be the doggy variable.
  3. We're saying that the pet object's breed property is the same as the doggy variable.
  4. We're saying that the doggy variable should be the pet object's breed property.
Array Destructuring
const animals = ['dog', 'cat', 'bird', 'monkey', 'elephant', 'tiger'];
const [first] = animals;
console.log(first);

Explanation:
  1. The first line of the code is declaring a variable called animals and assigning it to an array.
  2. The second line is declaring a variable called first and assigning it to an array.
  3. The third line is a destructuring assignment.
  4. The first part of the destructuring assignment is assigning the first element of animals to first.

Summary: The first value in the array should be assigned to the varible called first.

More example
const animals = ['dog', 'cat', 'bird', 'monkey', 'elephant', 'tiger'];
const [first, second] = animals;
console.log(first, second);

Explanation:
The first two elements are assigned to the variable.First and Second

More Examples
const animals = ['dog', 'cat', 'bird', 'monkey', 'elephant', 'tiger'];
const [, , third] = animals;
console.log(third);


Explanation:
1. The variable animals contains an array of strings.
2. The variable [, , third] is an array with three elements.
3. The variable third is the third element of the variable [, , third], which is the string 'bird'.
4. The `, ,` skips the first two elements and puts the third value inside the variable called <mark>third</mark>

You can rejig the above code

const [, , third] = ['dog', 'cat', 'bird', 'monkey', 'elephant', 'tiger'];
console.log(third);

Discuss

In what situations do you use destructuring of arrays and objects?
Please leave a comment in the section below.


This content originally appeared on DEV Community and was authored by Abayomi Ogunnusi


Print Share Comment Cite Upload Translate Updates
APA

Abayomi Ogunnusi | Sciencx (2022-04-17T15:03:50+00:00) Learn Javascript Destructuring using Github Copilot Labs. Retrieved from https://www.scien.cx/2022/04/17/learn-javascript-destructuring-using-github-copilot-labs/

MLA
" » Learn Javascript Destructuring using Github Copilot Labs." Abayomi Ogunnusi | Sciencx - Sunday April 17, 2022, https://www.scien.cx/2022/04/17/learn-javascript-destructuring-using-github-copilot-labs/
HARVARD
Abayomi Ogunnusi | Sciencx Sunday April 17, 2022 » Learn Javascript Destructuring using Github Copilot Labs., viewed ,<https://www.scien.cx/2022/04/17/learn-javascript-destructuring-using-github-copilot-labs/>
VANCOUVER
Abayomi Ogunnusi | Sciencx - » Learn Javascript Destructuring using Github Copilot Labs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/17/learn-javascript-destructuring-using-github-copilot-labs/
CHICAGO
" » Learn Javascript Destructuring using Github Copilot Labs." Abayomi Ogunnusi | Sciencx - Accessed . https://www.scien.cx/2022/04/17/learn-javascript-destructuring-using-github-copilot-labs/
IEEE
" » Learn Javascript Destructuring using Github Copilot Labs." Abayomi Ogunnusi | Sciencx [Online]. Available: https://www.scien.cx/2022/04/17/learn-javascript-destructuring-using-github-copilot-labs/. [Accessed: ]
rf:citation
» Learn Javascript Destructuring using Github Copilot Labs | Abayomi Ogunnusi | Sciencx | https://www.scien.cx/2022/04/17/learn-javascript-destructuring-using-github-copilot-labs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.