简体   繁体   English

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

[英]C# find differences between arrays of objects

I have an array called props that contains n number of arrays with objects and all arrays contain the same number of objects. 我有一个名为props的数组,其中包含n个对象数组,所有数组包含相同数量的对象。

Each object has 4 properties : participation_enabled, name, pathing_enabled, id and these properties can have different values in the other arrays for the same property id... 每个对象都具有4个属性:session_enabled,name,pathing_enabled,id,并且对于相同的属性id,这些属性在其他数组中可以具有不同的值...

My goal is to find all object properties that are different in the other arrays of objects and store them in another array called diffs. 我的目标是找到在其他对象数组中不同的所有对象属性,并将它们存储在另一个名为diffs的数组中。

Example: 例:

[
  [
    {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"}
  ]
]

After execution, the diff array should be: 执行后, 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}
]

This is how it's implemented using javascript with underscoreJS : 这是使用带有underscoreJS javascript来实现的:

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);

Any advice and ideas would help me a lot... 任何建议和想法都会对我有很大帮助。

Have a look at IEqualityComparer ( https://msdn.microsoft.com/it-it/library/ms132151(v=vs.110).aspx ) and Except ( https://msdn.microsoft.com/it-it/library/bb336390(v=vs.110).aspx ). 看看IEqualityComparerhttps://msdn.microsoft.com/it-it/library/ms132151 ( v= IEqualityComparer ) IEqualityComparer )和Excepthttps://msdn.microsoft.com/it-it/库/bb336390(v=vs.110).aspx )。

An approach could be creating a custom comparer and comparing your data two by two. 一种方法可能是创建一个自定义比较器,然后将数据两两比较。 Or maybe you could do a group by on all the elements excluding the ones with more occurrences. 或者,您可以对所有元素进行分组,但不包括出现次数更多的元素。

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

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