简体   繁体   中英

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.

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...

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.

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:

[
  {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 :

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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