对象数组是不同的,
某些对象 Module_Id 是相同的,如何在预期输出中合并相同的module_Id对象
[{
"Module_Id": "5b3c8f83f632ce0b049b16d0",
"Create_Permission": false,
"Edit_Permission": true,
"View_Permission": true,
"Delete_Permission": false
},
{
"Module_Id": "5b3c8f83f632ce0b049b16d0",
"Create_Permission": true,
"Edit_Permission": false,
"View_Permission": true,
"Delete_Permission": false
},
{
"Module_Id": "5b3c8f83f632ce0b049b16d0",
"Create_Permission": false,
"Edit_Permission": false,
"View_Permission": true,
"Delete_Permission": true
},
{
"Module_Id": "5b3c8f83f632ce0b049bg56d",
"Create_Permission": false,
"Edit_Permission": false,
"View_Permission": false,
"Delete_Permission": true
}]
我的预期输出是:
[{
"Module_Id": "5b3c8f83f632ce0b049b16d0",
"Create_Permission": true,
"Edit_Permission": true,
"View_Permission": true,
"Delete_Permission": true
},
{
"Module_Id": "5b3c8f83f632ce0b049bg56d",
"Create_Permission": false,
"Edit_Permission": false,
"View_Permission": false,
"Delete_Permission": true
}]
我正在使用NodeJ和ES6 javascript
答案 0 :(得分:1)
您需要使用Array.reduce()
:
var array = [{
"Module_Id": "5b3c8f83f632ce0b049b16d0",
"Create_Permission": false,
"Edit_Permission": true,
"View_Permission": true,
"Delete_Permission": false
},
{
"Module_Id": "5b3c8f83f632ce0b049b16d0",
"Create_Permission": true,
"Edit_Permission": false,
"View_Permission": true,
"Delete_Permission": false
},
{
"Module_Id": "5b3c8f83f632ce0b049b16d0",
"Create_Permission": false,
"Edit_Permission": false,
"View_Permission": true,
"Delete_Permission": true
},
{
"Module_Id": "5b3c8f83f632ce0b049bg56d",
"Create_Permission": false,
"Edit_Permission": false,
"View_Permission": false,
"Delete_Permission": true
}];
var res = array.reduce((acc, obj)=>{
let existObj = acc.find(({Module_Id}) => Module_Id == obj.Module_Id);
if(existObj){
existObj.Create_Permission = existObj.Create_Permission || obj.Create_Permission;
existObj.Edit_Permission = existObj.Edit_Permission || obj.Edit_Permission;
existObj.View_Permission = existObj.View_Permission || obj.View_Permission;
existObj.Delete_Permission = existObj.Delete_Permission || obj.Delete_Permission;
return acc;
}
acc.push(obj);
return acc;
}, []);
console.log(res);
答案 1 :(得分:0)
您可以将array#reduce
与Object.values()
一起使用
const data = [{ "Module_Id": "5b3c8f83f632ce0b049b16d0", "Create_Permission": false, "Edit_Permission": true, "View_Permission": true, "Delete_Permission": false }, { "Module_Id": "5b3c8f83f632ce0b049b16d0", "Create_Permission": true, "Edit_Permission": false, "View_Permission": true, "Delete_Permission": false }, { "Module_Id": "5b3c8f83f632ce0b049b16d0", "Create_Permission": false, "Edit_Permission": false, "View_Permission": true, "Delete_Permission": true }, { "Module_Id": "5b3c8f83f632ce0b049bg56d", "Create_Permission": false, "Edit_Permission": false, "View_Permission": false, "Delete_Permission": true }],
result = Object.values(data.reduce((r,o) => {
r[o.Module_Id] = o.Module_Id in r ? Object.keys(r[o.Module_Id]).reduce((res,k) => {
res[k] = r[o.Module_Id][k] || o[k];
return res;
},{}) : o;
return r;
},{}));
console.log(result);