var arr=[{
"PMC MATERIAL CONTROL":
[{
"userCode": "11084876",
"userId": "OR:wt.org.WTUser:9814145",
"userName": "陈让香"
}, {
"userCode": "11003474",
"userId": "OR:wt.org.WTUser:342317",
"userName": "刘斌"
}]
},{
"PMC MATERIAL CONTROL":
[{
"userCode": "11084876",
"userId": "OR:wt.org.WTUser:9814146",
"userName": "陈让香2"
}, {
"userCode": "11003474",
"userId": "OR:wt.org.WTUser:342317",
"userName": "刘斌"
}],
"PMC MATERIAL CONTROL2":
[{
"userCode": "11003474",
"userId": "OR:wt.org.WTUser:342317",
"userName": "刘斌"
}]
}]
变成
[{
"PMC MATERIAL CONTROL":
[{
"userCode": "11084876",
"userId": "OR:wt.org.WTUser:9814145",
"userName": "陈让香"
},{
"userCode": "11084876",
"userId": "OR:wt.org.WTUser:9814146",
"userName": "陈让香2"
}, {
"userCode": "11003474",
"userId": "OR:wt.org.WTUser:342317",
"userName": "刘斌"
}],
"PMC MATERIAL CONTROL2":
[{
"userCode": "11003474",
"userId": "OR:wt.org.WTUser:342317",
"userName": "刘斌"
}]
}]
===============方法================
Fun(){
var data = this.mergeData(arr);
console.log(data)
},
reduceFn(arr,select){
return arr.reduce((pre,cur)=>{
if(select.every(item=>item.userId!=cur.userId)){
pre.push(cur)
}
return pre;
},[]);
},
mergeData(arr){
var map = {};
for(var i in arr){
var item= arr[i];
for(var name in item){
if(!map[name]){
map[name] = item[name];
}else{
let difference = this.reduceFn(item[name],map[name])||{}
map[name]=[...map[name],...difference];
}
}
}
return map
},