Sunday, 13 November 2022

ES6 Destructuring

Destructuring in ES6 (ECMAScript 2015) is a feature that allows you to unpack values from arrays or properties from objects into distinct variables. It provides a more concise and readable way to extract data compared to traditional methods. Here's a breakdown of how destructuring works for arrays and objects:

Array Destructuring

Array destructuring allows you to extract values from an array and assign them to variables.

  const [a, b, c] = [1, 2, 3];
  console.log(a); // 1
  console.log(b); // 2
  console.log(c); // 3

Skipping Items: You can skip items in the array by leaving the position empty:

  const [first, , third] = [1, 2, 3];
  console.log(first); // 1
  console.log(third); // 3

Default Values: You can provide default values for variables:

  const [a = 1, b = 2] = [undefined, 3];
  console.log(a); // 1 (default value)
  console.log(b); // 3

Rest Operator: The rest operator (...) can be used to collect remaining elements into an array:

  const [first, ...rest] = [1, 2, 3, 4, 5];
  console.log(first); // 1
  console.log(rest);  // [2, 3, 4, 5]

Object Destructuring

Object destructuring allows you to extract properties from an object and assign them to variables.

  const { name, age } = { name: 'John', age: 30 };
  console.log(name); // John
  console.log(age);  // 30

Default Values: You can provide default values for variables:

  const { name = 'Anonymous', age = 25 } = { name: 'John' };
  console.log(name); // John
  console.log(age);  // 25 (default value)

Renaming Variables: You can rename variables while destructuring:

  const { name: userName, age: userAge } = { name: 'John', age: 30 };
  console.log(userName); // John
  console.log(userAge);  // 30

Nested Destructuring: You can destructure nested objects or arrays:

  const person = {
    name: 'John',
    address: {
      city: 'New York',
      zip: '10001'
    }
  };
 
  const { name, address: { city, zip } } = person;
  console.log(name); // John
  console.log(city); // New York
  console.log(zip);  // 10001

Rest Operator in Objects: The rest operator can also be used to collect remaining properties into a new object:

  const { name, ...rest } = { name: 'John', age: 30, city: 'New York' };
  console.log(name); // John
  console.log(rest); // { age: 30, city: 'New York' }

Destructuring can make your code more readable and reduce the need for repetitive property access or array indexing.

Related Posts:

  • Javascript Vs JqueryJavaScript: A powerful language in web developmentJavaScript is a scripting language that is used to add interactivity to our web pages. It is one of … Read More
  • Javascript: Map and SetNow we’ve learned about the following complex data structures:Objects for storing keyed collections.Arrays for storing ordered collections.But that’s … Read More
  • UI Framework Questions What is React JS? ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page appl… Read More
  • map(), filter(), and reduce() in JavaScriptmap → Executes a function on each element of an arrayEvery element of the array is passed to the callback function and returns a new array with the sa… Read More
  • Dirty Forms Prevent users from losing data when editing forms: Dirty Forms is a flexible jQuery plugin to help prevent users from losing data when editing for… Read More

0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives