C#查找对象数组之间的差异

时间:2017-04-05 07:38:01

标签: c# arrays

我有一个名为props的数组,其中包含n个带对象的数组,所有数组都包含相同数量的对象。

每个对象都有4个属性:participant_enabled,name,pathing_enabled,id,这些属性可以在其他数组中具有相同属性id的不同值...

我的目标是找到其他对象数组中不同的所有对象属性,并将它们存储在另一个名为diffs的数组中。

示例:

[
  [
    {participation_enabled:"false", name:"PropEins", pathing_enabled:"true", id:"prop1"}, 
    {participation_enabled:"false", name:"User Status", pathing_enabled:"false", id:"prop2"}, 
    {participation_enabled:"false", name:"Initial ID", pathing_enabled:"false", id:"prop3"}, 
    {participation_enabled:"false", name:"User ID", pathing_enabled:"false", id:"prop4"}, 
    {participation_enabled:"false", name:"Subdomain", pathing_enabled:"false", id:"prop5"}
  ], 
  [
    {participation_enabled:"false", name:"PropEins", pathing_enabled:"false", id:"prop1"}, 
    {participation_enabled:"false", name:"Room", pathing_enabled:"false", id:"prop2"}, 
    {participation_enabled:"false", name:"Phase", pathing_enabled:"false", id:"prop3"}, 
    {participation_enabled:"false", name:"Custom Insight 4", pathing_enabled:"false", id:"prop4"}, 
    {participation_enabled:"false", name:"Subdomain", pathing_enabled:"false", id:"prop5"}
  ], 
  [
    {participation_enabled:"true", name:"PropEins", pathing_enabled:"true", id:"prop1"}, 
    {participation_enabled:"true", name:"User Status", pathing_enabled:"true", id:"prop2"}, 
    {participation_enabled:"true", name:"Trackingcode", pathing_enabled:"true", id:"prop3"}, 
    {participation_enabled:"false", name:"User ID", pathing_enabled:"false", id:"prop4"}, 
    {participation_enabled:"false", name:"Subdomain", pathing_enabled:"false", id:"prop5"}
  ]
]

执行后,diff数组应为:

[
  {id:"prop1", participation_enabled:["false","true"], pathing_enabled:["false","true"], index:0},
  {id:"prop2", participation_enabled:["false","true"], name:["User Status","Room"], participation_enabled:["false","true"], pathing_enabled:["false","true"], index:1},
  {id:"prop3", participation_enabled:["false","true"], name:["Initial ID","Phase","Trackingcode"], participation_enabled:["false","true"], pathing_enabled:["false","true"], index:2},
  {id:"prop4", name:["User ID","Custom Insight 4"], pathing_enabled:["false","true"], index:3}
]

这是使用javascriptunderscoreJS实现的方式:

var diff = {};
a.forEach(function(val, i){
  //first just init start object
  if (i == 0) {
    val.forEach(function(v1, ind){
      diff[v1.id] = {};
      diff[v1.id].index = [ind];
      for (var key in v1) {
        diff[v1.id][key] = [v1[key]];
      }
    });
  }
  else {
    //for all other values add them into array and remove dups
    val.forEach(function(v1){
      var id = v1.id;
      for (var key in v1) {
        diff[id][key].push(v1[key]);
      }
    });
  }
});

//now finalize data removing all that have only unique values
for (var key in diff) {
  var nested = diff[key];
  var index = nested.index.pop();
  for (nestedKey in nested) {
    nested[nestedKey] =  _.filter(nested[nestedKey], function(item, pos) {
      return nested[nestedKey].indexOf(item) == pos;
    });

    if (nested[nestedKey].length < 2) {delete nested[nestedKey];}

  }
  diff[key].id = key;
  diff[key].index = index
  if (_.keys(diff[key]).length < 3) {delete diff[key];}
}

diff = _.values(diff);

任何建议和想法都会对我有所帮助......

1 个答案:

答案 0 :(得分:0)

查看IEqualityComparerhttps://msdn.microsoft.com/it-it/library/ms132151(v=vs.110).aspx)和Excepthttps://msdn.microsoft.com/it-it/library/bb336390(v=vs.110).aspx)。

一种方法可能是创建自定义比较器并逐个比较您的数据。或者也许你可以对所有元素进行分组,排除出现次数较多的元素。