在n个对象数组之间查找差异(使用其他对象)

时间:2016-10-28 09:19:01

标签: javascript arrays google-apps-script

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

每个对象都有4个属性:participation_enablednamepathing_enabledid这些属性可以在其他数组中为相同的属性ID具有不同的值。

我有两个目标:
1)找到其他对象数组中不同的所有对象属性,并将它们存储在另一个名为diffs的数组中;
2)找到在其他数组中无法找到的所有额外对象(唯一id)并将它们存储在另一个名为extra的数组中。

我的代码成功返回差异,但它不会返回所有额外的对象......

示例:

[
  [
    {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:"Subdomain3", pathing_enabled:"false", id:"prop20"},
    {participation_enabled:"false", name:"Subdomain4", pathing_enabled:"false", id:"prop21"},
    {participation_enabled:"false", name:"Subdomain5", pathing_enabled:"false", id:"prop22"}
  ], 
  [
    {participation_enabled:"false", name:"PropEins", pathing_enabled:"false", id:"prop1"}, 
    {participation_enabled:"false", name:"Room", pathing_enabled:"false", id:"prop2"}, 
    {participation_enabled:"false", name:"Subdomain", pathing_enabled:"false", id:"prop70"}
  ], 
  [
    {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:"true", id:"prop4"}, 
    {participation_enabled:"false", name:"User ID2", pathing_enabled:"false", id:"prop50"}
  ]
]

对于此示例,diffs数组应包含:

[
  {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","Trackingcode"], participation_enabled:["false","true"], pathing_enabled:["false","true"], index:2},
  {id:"prop4", pathing_enabled:["false","true"], index:3}
]

extra数组应为:

[
  {participation_enabled:"false", name:"Subdomain3", pathing_enabled:"false", id:"prop20"},
  {participation_enabled:"false", name:"Subdomain4", pathing_enabled:"false", id:"prop21"},
  {participation_enabled:"false", name:"Subdomain5", pathing_enabled:"false", id:"prop22"},
  {participation_enabled:"false", name:"Subdomain", pathing_enabled:"false", id:"prop70"},
  {participation_enabled:"false", name:"User ID2", pathing_enabled:"false", id:"prop50"}
]

这是我的代码:

  var diff = {};
  var extra = [];
  props.forEach(function(val, i){
    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 {
      val.forEach(function(v1){
        var id = v1.id;
        for (var key in v1) {
          if(diff[id]) {
            diff[id][key].push(v1[key]);
          }
        }
        if(!diff[id]) {
          extra.push(v1);
        }
      });
    }
  });

  for (var key in diff) {
    var nested = diff[key];
    var index = nested.index.pop();
    for (nestedKey in nested) {
      nested[nestedKey] =  underscoreGS._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 (underscoreGS._keys(diff[key]).length < 3) {delete diff[key];}
  }

  diff = underscoreGS._values(diff);
  return [diff, extra];

1 个答案:

答案 0 :(得分:0)

这是我实施和运作的解决方案:

&#13;
&#13;
var variable = [
  [
    {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:"Subdomain3", pathing_enabled:"false", id:"prop20"},
    {participation_enabled:"false", name:"Subdomain4", pathing_enabled:"false", id:"prop21"},
    {participation_enabled:"false", name:"Subdomain5", pathing_enabled:"false", id:"prop22"}
  ], 
  [
    {participation_enabled:"false", name:"PropEins", pathing_enabled:"false", id:"prop1"}, 
    {participation_enabled:"false", name:"Room", pathing_enabled:"false", id:"prop2"}, 
    {participation_enabled:"false", name:"Subdomain", pathing_enabled:"false", id:"prop70"}
  ], 
  [
    {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:"true", id:"prop4"}, 
    {participation_enabled:"false", name:"User ID2", pathing_enabled:"false", id:"prop50"}
  ]
];
  var diff = {};
  var extra = [];
  var ids = [];
  var unique = [];
  
  variable.forEach(function(val){
    val.forEach(function(v1) {
      ids.push(v1.id);
    });
  })
  ids.filter(function(a){
    if(ids.indexOf(a) == ids.lastIndexOf(a)) {
      unique.push(a);
    }
  });
  variable.forEach(function(val){
    val.forEach(function(v1){
      if(unique.indexOf(v1.id) >= 0) {
        
        extra.push(v1);
      }    
    });
  });
  
  variable.forEach(function(val, i){
    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 {
      val.forEach(function(v1){
        var id = v1.id;
        for (var key in v1) {
          if(diff[id]) {
            diff[id][key].push(v1[key]);
          }
        }
      });
    }
  });
  
  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);
  console.log("extra:", extra);
  console.log("diff:", diff);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;