最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compare two objects properties in JS - Stack Overflow

programmeradmin9浏览0评论

I need to pare 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 pare is just the properties. I do not care for the value. I will be paring this object with another one very similar, but some properties might be missing.

I need to pare 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 pare is just the properties. I do not care for the value. I will be paring this object with another one very similar, but some properties might be missing.

Share Improve this question edited Sep 5, 2017 at 16:02 Billal BEGUERADJ 22.8k45 gold badges123 silver badges140 bronze badges asked Sep 5, 2017 at 16:01 Paulo Borralho MartinsPaulo Borralho Martins 3503 silver badges11 bronze badges 3
  • What will you be doing with this parison ? We can help provide a solution based on your needs, and a little more information on your end goal will help. – Christopher Messer Commented Sep 5, 2017 at 16:04
  • 2 Are you expecting this to be n level check? – Amit Kumar Singh Commented Sep 5, 2017 at 16:08
  • So one object is sent from the server, and carries several strings to be used in the html content. If some of the properties are missing, i need to stop the code, show an error page, and show all the properties that are missing – Paulo Borralho Martins Commented Sep 5, 2017 at 16:09
Add a ment  | 

5 Answers 5

Reset to default 5

function pare(base, pared, deepSearch) {
  var missing = [];

  var pareProp = function (baseValue, paredValue, path, deepSearch) {
    //console.log('paring', path.join('.'));

    if (paredValue === undefined) {
      console.log('missing key', path.join('.'));

        if (!deepSearch) {
          return;
        }
    }

    if (typeof baseValue === 'object') {
      Object.keys(baseValue).forEach(function (key) {
        pareProp(baseValue [key], paredValue && paredValue [key], path.concat(key), deepSearch);
      }); 
    }
  };

  Object.keys(base).forEach(function (key) {
    pareProp(base [key], pared [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 = {};

pare (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 parison 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);

How about use JSON.stringify to convert object to string, then do the string parison:

JSON.stringify(UC) === JSON.stringify(UCToBeCompared)

By using this method, you have to make sure the objects don't have circular reference, otherwise JSON.stringify would throw an exception

If your situation allows it I'd suggest using http://underscorejs/ library, rather than rolling your own solution (or go look at their implementation). In JS deep object parison is sometimes not trivial. If you decide to roll your own solution, you would recursively iterate through the properties and pare them one by one (ignoring native / built-in object properties and perhaps inherited from some prototype).

I'll gladly elaborate if you'd like.

I have a made a example here. Hope it resolves your issue. It will pare Object KEYS only and return the object key which is not exist with pared object.

var a = Object.keys(obj1);
var b = Object.keys(obj2);

var missing= a.filter(function(v){
    return b.indexOf(v)==-1;
})

console.log(missing);
发布评论

评论列表(0)

  1. 暂无评论