javascript - 如何获得两个 JavaScript 对象图之间差异的列表?

标签 javascript json data-structures diff

我希望能够获得两个 JavaScript 对象图之间所有差异的列表,以及出现增量的属性名称和值。

就其值(value)而言,这些对象通常以 JSON 格式从服务器检索,并且通常不超过几层深(即它可能是一个本身具有数据的对象数组,然后是具有其他数据对象的数组).

我不仅想看到基本属性的变化,还想看到数组成员数量的差异等等。

如果我没有得到答案,我可能最终会自己写这篇文章,但希望有人已经做过这项工作或知道有人做过。


编辑:这些对象通常在结构上彼此非常接近,所以我们不是在谈论彼此完全不同的对象,但可能有 3 或 4 个增量。

最佳答案

这是针对我的问题的部分、天真的解决方案 - 我会在进一步开发时更新它。

function findDifferences(objectA, objectB) {
   var propertyChanges = [];
   var objectGraphPath = ["this"];
   (function(a, b) {
      if(a.constructor == Array) {
         // BIG assumptions here: That both arrays are same length, that
         // the members of those arrays are _essentially_ the same, and 
         // that those array members are in the same order...
         for(var i = 0; i < a.length; i++) {
            objectGraphPath.push("[" + i.toString() + "]");
            arguments.callee(a[i], b[i]);
            objectGraphPath.pop();
         }
      } else if(a.constructor == Object || (a.constructor != Number && 
                a.constructor != String && a.constructor != Date && 
                a.constructor != RegExp && a.constructor != Function &&
                a.constructor != Boolean)) {
         // we can safely assume that the objects have the 
         // same property lists, else why compare them?
         for(var property in a) {
            objectGraphPath.push(("." + property));
            if(a[property].constructor != Function) {
               arguments.callee(a[property], b[property]);
            }
            objectGraphPath.pop();
         }
      } else if(a.constructor != Function) { // filter out functions
         if(a != b) {
            propertyChanges.push({ "Property": objectGraphPath.join(""), "ObjectA": a, "ObjectB": b });
         }
      }
   })(objectA, objectB);
   return propertyChanges;
}

这里是一个如何使用它以及它将提供的数据的示例(请原谅这个冗长的示例,但我想使用一些相对重要的东西):

var person1 = { 
   FirstName : "John", 
   LastName : "Doh", 
   Age : 30, 
   EMailAddresses : [
      "john.doe@gmail.com", 
      "jd@initials.com"
   ], 
   Children : [ 
      { 
         FirstName : "Sara", 
         LastName : "Doe", 
         Age : 2 
      }, { 
         FirstName : "Beth", 
         LastName : "Doe", 
         Age : 5 
      } 
   ] 
};

var person2 = { 
   FirstName : "John", 
   LastName : "Doe", 
   Age : 33, 
   EMailAddresses : [
      "john.doe@gmail.com", 
      "jdoe@hotmail.com"
   ], 
   Children : [ 
      { 
         FirstName : "Sara", 
         LastName : "Doe", 
         Age : 3 
      }, { 
         FirstName : "Bethany", 
         LastName : "Doe", 
         Age : 5 
      } 
   ] 
};

var differences = findDifferences(person1, person2);

此时,如果您将它序列化为 JSON,differences 数组将如下所示:

[
   {
      "Property":"this.LastName", 
      "ObjectA":"Doh", 
      "ObjectB":"Doe"
   }, {
      "Property":"this.Age", 
      "ObjectA":30, 
      "ObjectB":33
   }, {
      "Property":"this.EMailAddresses[1]", 
      "ObjectA":"jd@initials.com", 
      "ObjectB":"jdoe@hotmail.com"
   }, {
      "Property":"this.Children[0].Age", 
      "ObjectA":2, 
      "ObjectB":3
   }, {
      "Property":"this.Children[1].FirstName", 
      "ObjectA":"Beth", 
      "ObjectB":"Bethany"
   }
]

Property 值中的this 指的是被比较对象的根。所以,这个解决方案还不是完全我所需要的,但已经非常接近了。

希望这对外面的人有用,如果您有任何改进建议,我会全神贯注;我昨晚很晚(即今天清晨)写了这篇文章,可能有些事情我完全忽略了。

谢谢。

关于javascript - 如何获得两个 JavaScript 对象图之间差异的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/264430/

相关文章:

jQuery、JSON、Flickr API

iOS 核心数据序列化

performance - 查找最大/最小连续异或值

data-structures - 在 golang 中实现嵌套矩阵的惯用方式

javascript - 删除本地化代码中的 JQuery 依赖

android - 检索 json 数据

javascript - 在 CKEditor 中键入时替换文本

data-structures - 堆栈、队列和链表

Javascript 替换

javascript - 是否可以模拟 iframe?