2017-04-05 87 views
0

我有一個名爲props的數組,其中包含n個具有對象的數組,並且所有數組都包含相同數量的對象。C#發現對象數組之間的差異

每個對象有4個屬性:participation_enabled,名稱,pathing_enabled,標識和這些屬性可以在其他陣列同一屬性的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); 

任何意見和想法會幫助我很多...

+0

你到目前爲止已經嘗試完成你的目標 – EpicKip

+0

我真的不知道從哪裏開始,因爲我之前沒有在C#中使用過對象...... – Valip

回答