title: JavaScript fill missing keys in one JSON object from another JSON object
body: <p>I have a JSON object <strong>object1</strong> that needs to have missing fields filled from <strong><em>object2</em></strong> - existing fields should not be replaced.</p>
  
  <p>I used to use this function:</p>
  
  <pre><code>function fillObject(from, to) {
      for (var key in from) {
          if (from.hasOwnProperty(key)) {
              if (Object.prototype.toString.call(from[key]) === '[object Object]') {
                  if (!to.hasOwnProperty(key)) {
                      to[key] = {};
                  }
                  fillObject(from[key], to[key]);
              }
              else if (!to.hasOwnProperty(key)) {
                  to[key] = from[key];
              }
          }
      }
  }
  </code></pre>
  
  <p>And it worked all the while both objects had the same structure. Now the <em>items</em> inside <strong><em>object1</em></strong> can actually appear anywhere in the structure. Example of <strong><em>object1</em></strong> and <strong><em>object2</em></strong> (structure might seem funny as I've removed all the unnecessary keys).</p>
  
  <pre><code>var object1 = [
      {
          "position": 1,
          "items": [
              { 
                  "position": 1, "itemId": 431
              },
              {
                  "position": 2, "itemId": 1162, "title": "Overwritten title"
              }
          ]
      },
      {
          "position": 2,
          "groups": [
              {
                  "position": 1
                  "items": [
                      {
                          "position": 1, "itemId": 452, "title": "New title"
                      },
                      {
                          "position": 2, "itemId": 1388
                      },
                      {
                          "position": 3, "itemId": 1942
                      }
                  ]
              },
              { 
                  "position": 2, "itemId": 1942 
              },
              {
                  "position": 3,
                  "items": [
                      {
                          "position": 1, "itemId": 431
                      },
                      {
                          "position": 2, "itemId": 2000
                      },
                      {
                          "position": 3, "itemId": 452
                      }
                  ]
              }
          ]
      },
      {
          "position": 3, "itemId": 1388
      },
      {
          "position": 4, "itemId": 2000, "title": "Extra title"
      }
  ];
  
  var object2 [
      { "itemId": 431, "title": "Title 1" },
      { "itemId": 452, "title": "Title 2" },
      { "itemId": 1162, "title": "Title 3" },
      { "itemId": 1388, "title": "Title 4" },
      { "itemId": 1942, "title": "Title 5" },
      { "itemId": 2000 }
  ];
  </code></pre>
  
  <p>And this is what I want the result to be:</p>
  
  <pre><code>var object1 = [
      {
          "position": 1,
          "items": [
              {
                  "position": 1, "itemId": 431, "title": "Title 1"
              },
              {
                  "position": 2, "itemId": 1162, "title": "Overwritten title"
              }
          ]
      },
      {
          "position": 2,
          "groups": [
              {
                  "position": 1,
                  "items": [
                      {
                          "position": 1, "itemId": 452, "title": "New title"
                      },
                      {
                          "position": 2, "itemId": 1388, "title": "Title 4"
                      },
                      {
                          "position": 3, "itemId": 1942, "title": "Title 5"
                      }
                  ]
              },
              {
                  "position": 2, "itemId": 1942, "title": "Title 5"
              },
              {
                  "position": 3,
                  "items": [
                      {
                          "position": 1, "itemId": 431, "title": "Title 1"
                      },
                      {
                          "position": 2, "itemId": 2000
                      },
                      {
                          "position": 3, "itemId": 452, "title": "Title 2"
                      },
                  ]
              }
          ]
      },
      {
          "position": 3, "itemId": 1388, "title": "Title 4"
      },
      {
          "position": 4, "itemId": 2000, "title": "Extra title"
      }
  ];
  </code></pre>
  
  <p>Thanks in advance for the help.</p>