title: JavaScript: Deep check objects have same keys
body: <p>Question is similar to: <a href="https://stackoverflow.com/questions/14368596/how-can-i-check-that-two-objects-have-the-same-set-of-property-names">How can I check that two objects have the same set of property names?</a> but only one difference</p>
  
  <p>I want to check:</p>
  
  <pre><code>var objOne = {"a":"one","b":"two","c":{"f":"three_one"}};
  var objTwo = {"a":"four","b":"five","c":{"f":"six_one"}};
  </code></pre>
  
  <p>have the same set of <strong>keys</strong> in all level? </p>
  
  <p>For example <code>deepCheckObjKeys(objOne, objTwo)</code> would return <code>true</code> where <code>deepCheckObjKeys(objOne, objThree)</code> return <code>false</code>, if:</p>
  
  <pre><code>var objThree = {"a":"four","b":"five","c":{"g":"six_one"}};
  </code></pre>
  
  <p>Since <code>objThree.a.c.f</code> is <code>undefined</code> in <strong>objThree</strong>.</p>
  
  <p>A function like this:</p>
  
  <p>'use strict';</p>
  
  <pre><code>function objectsHaveSameKeys() {
     for (var _len = arguments.length, objects = Array(_len), _key = 0; _key &lt; _len; _key++) {
        objects[_key] = arguments[_key];
     }
  
     var allKeys = objects.reduce(function (keys, object) {
        return keys.concat(Object.keys(object));
     }, []);
     var union = new Set(allKeys);
     return objects.every(function (object) {
        return union.size === Object.keys(object).length;
     });
  }
  </code></pre>
  
  <p>only checks the first level.</p>
  
  <p><strong>PS: objectsHaveSameKeys() ES6 equivalent:</strong> </p>
  
  <pre><code>function objectsHaveSameKeys(...objects):boolean {
     const allKeys = objects.reduce((keys, object) =&gt; keys.concat(Object.keys(object)), []);
     const union = new Set(allKeys);
     return objects.every(object =&gt; union.size === Object.keys(object).length);
  }
  </code></pre>