How to get single object from this array of objects, and if there is true in some of the property it should be set to true.
For example if BILLING_CYCLE for view property has true in one of objects the returned object should have true value.
As you see groups can be nested, so recursion is needed. These are interfaces of object
interface UserPermissions {
[key: string]: UserPermission;
}
interface UserPermission {
view: boolean;
modify: boolean;
delete?: boolean;
additionalActions?: { [key: string]: boolean };
groups?: { [key: string]: UserPermission };
}
const data = [
{
"BILLING": {
"view": false,
"modify": false,
"additionalActions": {},
"groups": {
"BILLING_CYCLE": {
"view": false,
"modify": false,
"additionalActions": {
"CONFIRM": false
},
"groups": {}
},
"ACCOUNT": {
"view": false,
"modify": false,
"additionalActions": {},
"groups": {}
},
"ADMIN": {
"view": true,
"modify": true,
"additionalActions": {},
"groups": {}
}
}
}
},
{
"BILLING": {
"view": true,
"modify": true,
"additionalActions": {},
"groups": {
"BILLING_CYCLE": {
"view": true,
"modify": true,
"additionalActions": {
"CONFIRM": true
},
"groups": {}
},
"ACCOUNT": {
"view": true,
"modify": true,
"additionalActions": {},
"groups": {}
},
"ADMIN": {
"view": false,
"modify": false,
"additionalActions": {},
"groups": {}
}
}
}
}
]
const expectation = {
"BILLING": {
"view": true,
"modify": true,
"additionalActions": {},
"groups": {
"BILLING_CYCLE": {
"view": true,
"modify": true,
"additionalActions": {
"CONFIRM": true
},
"groups": {}
},
"ACCOUNT": {
"view": true,
"modify": true,
"additionalActions": {},
"groups": {}
},
"ADMIN": {
"view": true,
"modify": true,
"additionalActions": {},
"groups": {}
}
}
}
}
}
I'd suggest creating a merging function, say, mergePermissions(), then call Array.reduce() on the input, merging each object with the accumulator object recursively.
For each property, we'll keep the existing value, unless it is a boolean. If so, we'll perform an or comparison (||), so if either if true then result will also be true.
const data = [ { "BILLING": { "view": false, "modify": false, "additionalActions": {}, "groups": { "BILLING_CYCLE": { "view": false, "modify": false, "additionalActions": { "CONFIRM": false }, "groups": {} }, "ACCOUNT": { "view": false, "modify": false, "additionalActions": {}, "groups": {} }, "ADMIN": { "view": true, "modify": true, "additionalActions": {}, "groups": {} } } } }, { "BILLING": { "view": true, "modify": true, "additionalActions": {}, "groups": { "BILLING_CYCLE": { "view": true, "modify": true, "additionalActions": { "CONFIRM": true }, "groups": {} }, "ACCOUNT": { "view": true, "modify": true, "additionalActions": {}, "groups": {} }, "ADMIN": { "view": false, "modify": false, "additionalActions": {}, "groups": {} } } } } ]
function mergePermissions(obj1, obj2) {
let result = {};
for(let k in obj1) {
if (obj1[k] && typeof(obj1[k]) === 'object') {
result[k] = mergePermissions(obj1[k], (obj2?.[k])) ;
} else if ((typeof(obj1[k]) === 'boolean') && (typeof(obj2?.[k]) === 'boolean')) {
result[k] = obj1[k] || (obj2?.[k]);
} else {
result[k] = obj1[k];
}
}
return result;
}
let result = data.reduce((acc, el) => {
return mergePermissions(el, acc);
}, {})
console.log('Result:', result);