title: Comparing the keys of two JavaScript objects
body: <p>I've got two sets of JavaScript objects. I want to compare object1 to object2, and then get a list of all the keys that are in object1, but not in object2. I've searching for resources to help me, but I've only ended up finding comparison functions for simple objects. The objects that I want to compare have a lot of nesting. I've included an example at the bottom.</p>
  
  <p>How would I go about making a function for comparing these two objects? Is it possible to create a flexible function, that would also work if the objects were to change and contain more nesting?</p>
  
  <pre><code>const object1 = {
      "gender": "man",
      "age": 33,
      "origin": "USA",
      "jobinfo": {
          "type": "teacher",
          "school": "Wisconsin"
      },
      "children": [
          {
              "name": "Daniel",
              "age": 12,
              "pets": [
                  {
                      "type": "cat", 
                      "name": "Willy",
                      "age": 2
                  },
                  {
                      "type": "dog", 
                      "name": "jimmie",
                      "age": 5
                  }
              ]
          },
          {
              "name": "Martin",
              "age": 14,
              "pets": [
                  {
                      "type": "bird", 
                      "name": "wagner",
                      "age": 12
                  }
              ]
          }
      ],
      "hobbies": {
          "type": "football",
          "sponsor": {
              "name": "Pepsi",
              "sponsorAmount": 1000,
              "contact": {
                  "name": "Leon",
                  "age": 59,
                  "children": [
                      {
                          "name": "James",
                          "pets": [
                              {
                                  "type": "dog",
                                  "age": 4
                              }
                          ]
                      }
                  ]
              }
          }
      }
  }
  
  const object2 = {
      "gender": "man",
      "jobinfo": {
          "type": "teacher"
      },
      "children": [
          {
              "name": "Daniel",
              "age": 12,
              "pets": [
                  {
                      "type": "cat", 
                      "name": "Willy",
                      "age": 2
                  },
                  {
                      "type": "dog", 
                      "name": "jimmie",
                      "age": 5
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>So what I want to achieve by comparing these two objects, is in this case to have an array return that consists of the keys that are in object1, but not object2. So the array would look something like this.</p>
  
  <pre><code>["age", "hobbies", "type", "sponsor", "name", "sponsorAmount", "contact", "name", "age", "children", "name", "pets", "type", "age"].
  </code></pre>
  
  <p>This is what I've gotten to so far. This is sort of working. But it's not printing out age for example, because age is a property that exists in multiple of the nested objects.
  jsfiddle: <a href="https://jsfiddle.net/rqdgojq2/" rel="nofollow noreferrer">https://jsfiddle.net/rqdgojq2/</a></p>
  
  <p>I've had a look at the following resources:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/25175871/4623493">https://stackoverflow.com/a/25175871/4623493</a></li>
  <li><a href="https://stackoverflow.com/a/21584651/4623493">https://stackoverflow.com/a/21584651/4623493</a></li>
  </ul>