<?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: Edward Ellington</title>
    <description>The latest articles on DEV Community by Edward Ellington (@eellin6).</description>
    <link>https://dev.to/eellin6</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%2F516350%2F9508fd8d-839c-4bfc-8188-49b155b83136.jpeg</url>
      <title>DEV Community: Edward Ellington</title>
      <link>https://dev.to/eellin6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eellin6"/>
    <language>en</language>
    <item>
      <title>Manipulating Keys in an Object Using Recursion</title>
      <dc:creator>Edward Ellington</dc:creator>
      <pubDate>Wed, 18 Nov 2020 15:55:32 +0000</pubDate>
      <link>https://dev.to/eellin6/manipulating-keys-in-an-object-using-recursion-2cb7</link>
      <guid>https://dev.to/eellin6/manipulating-keys-in-an-object-using-recursion-2cb7</guid>
      <description>&lt;p&gt;It is no secret that Javascript was designed for object-oriented programming. Objects have the ability to store string, numeric and boolean values as well as functions (methods) in an easy to read and interpret way. Because understanding objects and how they work, or can be accessed, is a necessity in Javascript I would like to share an example of how you can manipulate an objects key to your will with the power of recursion!&lt;/p&gt;

&lt;h3&gt;
  
  
  Recursively Replacing a Key in an Object
&lt;/h3&gt;

&lt;p&gt;In the event that you may need to replace a key in an object, you can use a short little function that cleverly uses recursion to replace the key in an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var replaceKeysInObj = (obj, key, newKey) =&amp;gt; {
     let newObj = {};
     for (var ogKey in obj){
       if(ogKey === key) {
         newObj[newKey] = obj[ogKey]
    } else if (typeof obj[ogKey] === 'object') {
      newObj[ogKey] = replaceKeysInObj(obj[ogKey], key, newKey);
    } else {
      newObj[ogKey] = obj[ogKey]
    }
  }
  return newObj
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Here is a breakdown of the above function&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; - &lt;em&gt;The three parameters are going to be the original object(obj), the key we are going to change(key), and the new key we are going to use to replace the old key(newKey)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;First&lt;/em&gt;&lt;/strong&gt;: we create a new variable(newObj)and set it to an object literal({}). We will use this new object literal for creating our new and improved key in the object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Second&lt;/em&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (var ogKey in obj){
       if(ogKey === key) {
         newObj[newKey] = obj[ogKey]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use a for-in loop to see if the key in the object(ogKey) matches the key we are looking to replace(key). If it does then we are setting the new object key to the old object key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Third&lt;/em&gt;&lt;/strong&gt;: &lt;em&gt;time to use recursion&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;else if (typeof obj[ogKey] === 'object') {
      newObj[ogKey] = replaceKeysInObj(obj[ogKey], key, newKey);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the type of value for the object's original key is an object, then we set the value equal to the recursive case so it will also look inside of that object which is not just helpful... it's AWESOME.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Forth&lt;/em&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;else {
      newObj[ogKey] = obj[ogKey]
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The final else, if the first two conditions aren't met, will set the new object's key/value equal to the original object's key/value. This will allow the rest of the object's keys that do not match the "newKey" to stay the same after the recursive calls.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Fifth &amp;amp; Final&lt;/em&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return newObj
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We return the new object that has our updated key inside.&lt;/p&gt;

&lt;p&gt;There is always another way to skin a cat (&lt;em&gt;lookup Object.assign&lt;/em&gt;) but this way allows you to check through nested objects with &lt;strong&gt;the power of recursion&lt;/strong&gt;! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
