<?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: Adjoa Ackomah Edwin</title>
    <description>The latest articles on DEV Community by Adjoa Ackomah Edwin (@adjoaedwin_74).</description>
    <link>https://dev.to/adjoaedwin_74</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%2F251231%2Fd54fe506-2521-4571-8ddc-5cd100d268ec.jpg</url>
      <title>DEV Community: Adjoa Ackomah Edwin</title>
      <link>https://dev.to/adjoaedwin_74</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adjoaedwin_74"/>
    <language>en</language>
    <item>
      <title>How To Make Changes to Objects in a Cleaner, Immutable Way</title>
      <dc:creator>Adjoa Ackomah Edwin</dc:creator>
      <pubDate>Fri, 18 Oct 2019 10:22:46 +0000</pubDate>
      <link>https://dev.to/adjoaedwin_74/how-to-make-changes-to-objects-in-a-cleaner-immutable-way-5f1</link>
      <guid>https://dev.to/adjoaedwin_74/how-to-make-changes-to-objects-in-a-cleaner-immutable-way-5f1</guid>
      <description>&lt;h4&gt;
  
  
  This article will give you some tips on how to change state in a cleaner way in your JavaScript programs and with the ES6 syntax.
&lt;/h4&gt;

&lt;p&gt;I’m assuming that you have a basic knowledge of JavaScript and its ES6 syntax or you have a little experience with using JavaScript.&lt;/p&gt;

&lt;p&gt;It’s always a good idea to make states in your application &lt;em&gt;immutable&lt;/em&gt;.&lt;br&gt;
&lt;em&gt;Immutable data&lt;/em&gt; means data that doesn’t change. An example is a string in JavaScript. Once you create a string, it never changes.&lt;/p&gt;

&lt;p&gt;Data that is immutable is simpler. By doing this, you are sure that your data won’t be changed somewhere else in the codebase. And that’s a good thing. You are much more confident that unexpected things won’t happen.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is State?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;State&lt;/em&gt; are the things your program remembers or the stuff that takes place over time that your program keeps track of. For example, in a to-do list, the things that the app needs to remember are: when new to-dos are added, when to-dos are updated, and when they are removed from the list. Keeping track of these changes is what is known as maintaining state in your app.&lt;/p&gt;

&lt;p&gt;So then how do we deal with data in an immutable way, without changing the data? Every program needs a way of keeping track of these changes, and if you can’t modify the state, how then do we maintain state in the program?&lt;/p&gt;

&lt;p&gt;I’ll show you how.&lt;/p&gt;

&lt;p&gt;Below is an object with two properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Adjoa&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns { id:1, name: 'Adjoa'};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, how might I take all the properties in the person and add another property to it, say gender, without mutating it?&lt;/p&gt;

&lt;p&gt;Let me introduce you to the Object Spread operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  Object Spread Operator
&lt;/h2&gt;

&lt;p&gt;The object spread operator allows us to spread or expand an object’s properties into a new object. It makes copies of all the properties from one object to another object. This is useful in a lot of ways!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;female&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPerson&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//{ id:1, name: 'Adjoa', gender: 'female'};&lt;/span&gt;
&lt;span class="c1"&gt;// Includes all properties from the person object.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We don’t need to manually create a new object, write all the properties in the first object into the second, and then add the new properties. This way is super simple and easier!&lt;/p&gt;

&lt;p&gt;Do you know that this same operator can also be used to override the properties of an object?&lt;/p&gt;

&lt;p&gt;Let me show you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updatedPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;newPerson&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;male&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updatedPerson&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns { id: 1, name: 'Adjoa', gender: 'male' };&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The value of gender has changed from female to male.&lt;/p&gt;

&lt;p&gt;We have seen how we can update an object by adding new properties by not modifying its content. One might ask, how then do I remove elements in an immutable way? Here comes destructuring and rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Remove Fields From an Object With Destructuring + Rest
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is destructuring?
&lt;/h3&gt;

&lt;p&gt;Destructing just allows us to assign object values to variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// using our earlier example&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updatedPerson&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns 'Adjoa';&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see from the example above, we used the same names for the variables as we did for the properties of the &lt;code&gt;updatedPerson&lt;/code&gt;. But you can also replace variables with names that differ from the properties of the object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updatePerson&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns 'Adjoa';&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  What is rest?
&lt;/h3&gt;

&lt;p&gt;Rest allows us to unpack all the remaining arguments or properties of an object to a variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// using the earlier person object&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// {name: 'Adjoa')&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  How to remove fields using destructuring + rest:
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;updatedPersonWithoutId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updatedPerson&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What is happening above?&lt;br&gt;
We’re pulling the &lt;code&gt;id&lt;/code&gt; out into its own constant and then whatever properties remain in the &lt;code&gt;updatedPerson&lt;/code&gt; is collected into the &lt;code&gt;updatedPersonWithoutId&lt;/code&gt; constant. In other words, we are copying all the properties of &lt;code&gt;updatedPerson&lt;/code&gt; except the id into the &lt;code&gt;updatedPersonWithoutId&lt;/code&gt; constant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updatedPersonWithoutId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// returns { name: 'Adjoa', gender: 'male' }&lt;/span&gt;

&lt;span class="c1"&gt;// id was not included in the object returned.&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updatedPerson&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { id:1, name: 'Adjoa', gender: 'male' }&lt;/span&gt;

&lt;span class="c1"&gt;// original object hasn't mutated but retains its old properties!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So we effectively added and removed properties from the object in a cleaner, immutable way!&lt;br&gt;
Next, I will write on modifying arrays in JavaScript.&lt;/p&gt;

&lt;p&gt;Thank you for reading. If you found this article helpful, please leave a comment.&lt;/p&gt;

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