title: How to compare and combine two objects
body: <p>I have two objects:</p>
  <pre><code>myObj1 = { &quot;id&quot;: 1, &quot;name&quot;: &quot;Jason&quot;, &quot;attributes&quot;:{&quot;dob&quot;: &quot;07/5/2021&quot;, &quot;email&quot;: &quot;jason@email.com&quot;} }; 
  myObj2 = { &quot;id&quot;: 2, &quot;name&quot;: &quot;John&quot;, &quot;attributes&quot;:{&quot;dob&quot;: &quot;07/5/2021&quot;, &quot;email&quot;: &quot;&quot;} };
  </code></pre>
  <p>I would like to combine them into an object that looks like this:</p>
  <pre><code>myObj3 = { &quot;name&quot;:{ 1: &quot;Jason&quot;, 2: &quot;John&quot;, comparison: &quot;Different&quot; }, &quot;attributes&quot;: { &quot;dob&quot;: { 1: &quot;07/5/2021&quot;, 2: &quot;07/5/2021&quot;, comparison: &quot;identical&quot; }, email: { 1: &quot;jason@email.com&quot;, 2: &quot;-&quot;, comparison: &quot;missing&quot; } } };
  </code></pre>
  <ul>
  <li>Combines them into a single object</li>
  <li>iterates through and compares each of the attributes</li>
  <li>Adds the result of the comparison to the object</li>
  </ul>
  <p>I found a few articles which use key value pairs which gets me some of the way but maybe there's a better way to do it?</p>
  <pre><code>// find keys 
  keyObj1 = Object.keys(myObj1); 
  keyObj2 = Object.keys(myObj2); 
       
  // find values
  valueObj1 = Object.values(myObj1); 
  valueObj2 = Object.values(myObj2); 
   
  // find max length to iterate    
  if(keyObj1.length &gt; keyObj2.length) { 
      var biggestKey = keyObj1.length; 
  } else { 
      var biggestKey = keyObj2.length; 
  } 
  
  // now compare their keys and values  
  for(var i=0; i&lt;biggestKey; i++) {
      if(keyObj1[i] == keyObj2[i] &amp;&amp; valueObj1[i] == valueObj2[i]) { 
          console.log('same:' + valueObj2[i]);
      } else { 
          // it prints keys have different values 
          console.log('different: '+ valueObj1[i] + '\ndifferent: '+ valueObj2[i] +'\n'); 
      } 
  }
  </code></pre>