I need to compare two objects, and find out what properties are missing. The objects are fairly big, with several levels.
I will give short example of the type of object:
    UC = {};
    UC.start = {}
    UC.start.enableHardEccDecline = '';
    UC.start.template = {};
    UC.start.template.ecc = '';
    UC.start.template.decline = {};
    UC.start.template.decline.title = '';
    UC.start.template.decline.body = '';
    UC.general = {};...
So this is an example object. What I need to compare is just the properties. I do not care for the value. I will be comparing this object with another one very similar, but some properties might be missing.
function compare(base, compared, deepSearch) {
  var missing = [];
  var compareProp = function (baseValue, comparedValue, path, deepSearch) {
    //console.log('comparing', path.join('.'));
    if (comparedValue === undefined) {
      console.log('missing key', path.join('.'));
        if (!deepSearch) {
          return;
        }
    }
    if (typeof baseValue === 'object') {
      Object.keys(baseValue).forEach(function (key) {
        compareProp(baseValue [key], comparedValue && comparedValue [key], path.concat(key), deepSearch);
      }); 
    }
  };
  Object.keys(base).forEach(function (key) {
    compareProp(base [key], compared [key], [key], deepSearch);
  });
}
UC = {};
UC.start = {}
UC.start.enableHardEccDecline = '';
UC.start.template = {};
UC.start.template.ecc = '';
UC.start.template.decline = {};
UC.start.template.decline.title = '';
UC.start.template.decline.body = '';
UC.general = {};
compare (UC, {}, true);
I have just made a quick example here, not sure exactly how you want to apply this, but I have added the missing items to an array, which is logging it.
Obj1 should be your standard comparison object, obj2 the one received from request.
var obj1 = {};
obj1.test1 = 0;
obj1.test2 = 0;
obj1.test2222 = 0;
obj1.testLoremIpsum = 0;
obj1.lalala = 0;
var obj2 = {};
obj2.test1 = 0;
obj2.test25 = 0;
obj2.lalala1 = 0;
var k , i = 0;
var missingProps = [];
for( i in obj1 )
{
    var isFound = false;
    for( k in obj2) if( i == k ) isFound = true;
    if(!isFound) missingProps.push( i );
}
console.log(missingProps);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With