简体   繁体   English

比较JS中两个对象的属性

[英]Compare two objects properties in JS

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. Obj1 应该是您的标准比较对象,obj2 是从请求中收到的对象。

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 comparison:如何使用JSON.stringify将对象转换为字符串,然后进行字符串比较:

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通过使用这种方法,你必须确保对象没有循环引用,否则JSON.stringify会抛出异常

I have a made a example here.我在这里做了一个例子。 Hope it resolves your issue.希望它能解决您的问题。 It will compare Object KEYS only and return the object key which is not exist with compared object.它将只比较对象KEYS并返回与比较对象不存在的对象键。

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

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

console.log(missing);

If your situation allows it I'd suggest using http://underscorejs.org/ library, rather than rolling your own solution (or go look at their implementation).如果您的情况允许,我建议您使用http://underscorejs.org/库,而不是推出您自己的解决方案(或查看他们的实现)。 In JS deep object comparison is sometimes not trivial.在 JS 中,深度对象比较有时并不简单。 If you decide to roll your own solution, you would recursively iterate through the properties and compare them one by one (ignoring native / built-in object properties and perhaps inherited from some prototype).如果您决定推出自己的解决方案,您将递归地遍历属性并一一比较它们(忽略本机/内置对象属性,并且可能从某个原型继承)。

I'll gladly elaborate if you'd like.如果您愿意,我很乐意详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM