2016-10-28 55 views
-1

我有一個名爲props的數組,其中包含n數組數量與對象,並且所有數組都可以包含不同數量的對象。發現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]; 
+0

什麼是投反對票呢? – Valip

回答

0

這是我實現和工程解決方案:

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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>