-1
我有一個名爲props
的數組,其中包含n
數組數量與對象,並且所有數組都可以包含不同數量的對象。發現n個對象數組之間的差異(包括其他對象)
每個對象有4個屬性:participation_enabled
,name
,pathing_enabled
,id
這些屬性可以在其他陣列同一屬性的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];
什麼是投反對票呢? – Valip