<?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: gavgrif</title>
    <description>The latest articles on DEV Community by gavgrif (@gavgrif).</description>
    <link>https://dev.to/gavgrif</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%2F476336%2F2d0fba8c-17a7-4efe-8b4c-5f3c4c19e71d.png</url>
      <title>DEV Community: gavgrif</title>
      <link>https://dev.to/gavgrif</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gavgrif"/>
    <language>en</language>
    <item>
      <title>Object destructuring for the newbie</title>
      <dc:creator>gavgrif</dc:creator>
      <pubDate>Thu, 15 Oct 2020 23:54:10 +0000</pubDate>
      <link>https://dev.to/gavgrif/object-destructuring-for-the-newbie-bhk</link>
      <guid>https://dev.to/gavgrif/object-destructuring-for-the-newbie-bhk</guid>
      <description>&lt;p&gt;Many of you will know this - but its something I have been using a lot lately and find to be very useful. so I thought I would share...&lt;/p&gt;

&lt;p&gt;ES6 has introduced a new way of declaring variables from objects &lt;/p&gt;

&lt;p&gt;consider this object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const merchant = {
      firstName: 'John',
      lastName: 'Smith',
      vendorAssignedID: '123456'
    };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could access these as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const firstName = merchant.firstName; // would assign a variable (firstName) to have the value of "John";
  const lastName = merchant.lastName; // would assign a variable (lastName) to have the value of "Smith";
  const vendorAssignedID = merchant.vendorAssignedID; // would assign a variable (vendorAssignedID) to have the value of "12345";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This causes repetition of the property name and is very verbose - that’s where the object destructuring syntax is useful: you can read a property and assign its value to a variable without duplicating the property name. More than that, you can read multiple properties from the same object in just one statement!&lt;/p&gt;

&lt;p&gt;To refactor the above script and apply the object destructuring to access the properties names :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const {  firstName, lastName, vendorAssignedID  } = merchant;

    console.log(firstName); // 'John'
    console.log(lastName); // 'Smith'
    console.log(vendorAssignedID); // '123456'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This statement defines the variables and assigns to them the values of the matching properties of the object.&lt;/p&gt;

&lt;p&gt;You can alias the variables if the property name is too long for easy use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const { firstName, lastName, vendorAssignedID: ID } = merchant;

    console.log(firstName); // 'John'
    console.log(lastName); // 'Smith'
    console.log(ID); // '123456'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can add a default value if the property name does not exist in the object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const { firstName, lastName, vendorAssignedID: ID = '123456' } = merchant;

    console.log(firstName); // 'John'
    console.log(lastName); // 'GRiffith'
    console.log(ID); // '123456'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is much more to discuss with what you can do with object destructuring - here are some links from a quick google search&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment&lt;/a&gt;&lt;br&gt;
  &lt;a href="https://dmitripavlutin.com/javascript-object-destructuring/"&gt;https://dmitripavlutin.com/javascript-object-destructuring/&lt;/a&gt;&lt;br&gt;
  &lt;a href="https://javascript.info/destructuring-assignment"&gt;https://javascript.info/destructuring-assignment&lt;/a&gt;&lt;br&gt;
  &lt;a href="https://codeburst.io/es6-destructuring-the-complete-guide-7f842d08b98f"&gt;https://codeburst.io/es6-destructuring-the-complete-guide-7f842d08b98f&lt;/a&gt;&lt;br&gt;
  &lt;a href="https://www.javascripttutorial.net/es6/javascript-object-destructuring/"&gt;https://www.javascripttutorial.net/es6/javascript-object-destructuring/&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
