Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda