Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How to merge array of objects in single array and leave wanted values TypeScript

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": {}
        }
      }
    }
  }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!