<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: archana85</title>
    <description>The latest articles on DEV Community by archana85 (@archana85).</description>
    <link>https://dev.to/archana85</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F286969%2F74d6bec1-3437-435f-8727-f2d2c9403ffa.png</url>
      <title>DEV Community: archana85</title>
      <link>https://dev.to/archana85</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/archana85"/>
    <language>en</language>
    <item>
      <title>ES6 Destructuring in Typescript</title>
      <dc:creator>archana85</dc:creator>
      <pubDate>Thu, 18 Jul 2024 12:56:51 +0000</pubDate>
      <link>https://dev.to/archana85/es6-destructuring-in-typescript-3bim</link>
      <guid>https://dev.to/archana85/es6-destructuring-in-typescript-3bim</guid>
      <description>&lt;p&gt;Destructuring makes it possible to unpack values from arrays, or properties from objects, into distinct variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Makes code concise and more readable.&lt;/li&gt;
&lt;li&gt;We can easily avoid repeated destructuring expression.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Some use cases&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To get values in variable from Objects, Array.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [1, 2, 3, 4, 5];
let [first, second, ...rest] = array;
console.log(first, second, rest);
//expected output: 1 2 [3,4,5]

let objectFoo = { foo: 'foo' };
let { foo: newVarName } = objectFoo;
console.log(newVarName);
//expected output: foo

// Nested extraction
let objectFooBar = { foo: { bar: 'bar' } };
let { foo: { bar } } = objectFooBar;
console.log(bar);
//expected output: bar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To change only desired property in an object.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  let array = [
    { a: 1, b: 2, c: 3 },
    { a: 4, b: 5, c: 6 },
    { a: 7, b: 8, c: 9 },
  ];
  let newArray = array.map(({ a, ...rest }, index) =&amp;gt; ({
    a: index + 10,
    ...rest,
  }));
  console.log(newArray);
/* expected output: [
  {
    "a": 10,
    "b": 2,
    "c": 3    
  },
  {
    "a": 11,
    "b": 5,
    "c": 6
  },
  {
    "a": 12,
    "b": 8,
    "c": 9
  }
]*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To extract values from parameters into standalone variables.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Object destructuring:
  const objectDestructure = ({ property }: { property: string }) =&amp;gt; {
    console.log(property);
  };
  objectDestructure({ property: 'foo' });
  //expected output: foo

  // Array destructuring:
  const arrayDestructure = ([item1, item2]: [string, string]) =&amp;gt; {
    console.log(item1 , item2);
  };
  arrayDestructure(['bar', 'baz']);
  //expected output: bar baz


// Assigning default values to destructured properties:
  const defaultValDestructure = ({
    foo = 'defaultFooVal',
    bar,
  }: {
    foo?: string;
    bar: string;
  }) =&amp;gt; {
    console.log(foo, bar);
  };
  defaultValDestructure({ bar: 'bar' });
//expected output: defaultFooVal bar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To get dynamic keys value from object.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = { a: 1, b: 2, c: 3 };
const key = 'c';
let { [key]: val } = obj;
console.log(val);
//expected output: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To swap values.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const b = [1, 2, 3, 4];
[b[0], b[2]] = [b[2], b[0]]; // swap index 0 and 2
console.log(b);
//expected output : [3,2,1,4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To get a subset of an object.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = { a: 5, b: 6, c: 7 };
const subset = (({ a, c }) =&amp;gt; ({ a, c }))(obj);
console.log(subset); 
// expected output : { a: 5, c: 7 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To do array to object conversion.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = ["2024", "17", "07"],
      date = (([year, day, month]) =&amp;gt; ({year, month, day}))(arr);
console.log(date);
/* expected output: {
  "year": "2024",
  "month": "07",
  "day": "17"
} */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To set default values in function.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function someName(element, input, settings={i:"#1d252c", i2:"#fff", ...input}){
    console.log(settings.i);
    console.log(settings.i2);
}
someName('hello', {i: '#123'});
someName('hello', {i2: '#123'});
/* expected output: 
#123
#fff
#1d252c
#123 
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To get properties such as length from an array, function name, number of arguments etc.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let arr = [1,2,3,4,5];
let {length} = arr;
console.log(length);
let func = function dummyFunc(a,b,c) {
  return 'A B and C';
}
let {name, length:funcLen} = func;
console.log(name, funcLen);
/* expected output : 
5
dummyFunc 3
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To combine arrays or objects.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//combining two arrays
const alphabets = ['A','B','C','D','E','F'];
const numbers = ['1','2','3','4','5','6'];
const newArray = [...alphabets, ...numbers]
console.log(newArray);
//expected output: ['A','B','C','D','E','F','1','2','3','4','5','6']

//combining two objects
const personObj = { firstname: "John", lastname: "Doe"};
const addressObj = { city: "some city", state: "some state" };
const combinedObj = {...personObj, ...addressObj};
console.log(combinedObj);
/* expected output: {
    "firstname": "John",
    "lastname": "Doe",
    "city": "some city",
    "state": "some state"
} */
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>destructuring</category>
      <category>es6</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
