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

339
Visualizações
Group object keys values in order to obtain a list of objects grouped by a key

I have an object like:

{
"Account Management": [
    {
        "rowId": 1288,
        "departmentExternalId": "7",
        "departmentName": "Account Management",
        "measureName": "Total Headcount Expense",
        "measureId": 15,
        "measureValue": 17282,
    },
    {
        "rowId": 1289,
        "departmentExternalId": "7",
        "departmentName": "Account Management",
        "measureName": "Salary",
        "measureId": 5,
        "measureValue": 11666,
    },
],
"Client Services General (COR)": [
    {
        "rowId": 1294,
        "departmentExternalId": "114",
        "departmentName": "Client Services General (COR)",
        "measureName": "Total Headcount",
        "measureId": 2,
        "measureValue": 1,
    },
    {
        "rowId": 1295,
        "departmentExternalId": "114",
        "departmentName": "Client Services General (COR)",
        "measureName": "Total Headcount",
        "measureId": 2,
        "measureValue": 100,
    }
]
}

Each object key represents the department, object obtained after grouping the data got from BE. Now I need to group each object inside department by measureId and count them to obtain something like:

[
  {department: "Account Management", measure15: 17282, measure5: 11666},
  {department: "Client Services General (COR)", measure2: 101}
]

I tried to group the grouped by department data again by measureId, but I here I have a blocker and don't know how to continue.

If anybody has any idea, please tell me.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The logic is to create an array of objects that store the

[{measureX: sumOfValuesX}, {measureY: sumOfValuesY, ...}] 

For each specific id of the corresponding department. Then, by destructuring the object and combining it with Object.assign(), you can flat that whole array and have all values available.

As seen in this approach with additional use of Object.entries() and reduce():

let obj={"Account Management":[{rowId:1288,departmentExternalId:"7",departmentName:"Account Management",measureName:"Total Headcount Expense",measureId:15,measureValue:17282},{rowId:1289,departmentExternalId:"7",departmentName:"Account Management",measureName:"Salary",measureId:5,measureValue:11666},],"Client Services General (COR)":[{rowId:1294,departmentExternalId:"114",departmentName:"Client Services General (COR)",measureName:"Total Headcount",measureId:2,measureValue:1},{rowId:1295,departmentExternalId:"114",departmentName:"Client Services General (COR)",measureName:"Total Headcount",measureId:2,measureValue:100}]}

output = Object.entries(obj).map(([key, value]) => ({
    "department": key,
    ...Object.assign(...value.reduce((acc, {measureId, measureValue} ) => {
       let measureIdKey = `measure${measureId}`
       let currId = acc.filter(x => x[measureIdKey])
       return (currId.length ?
            (currId[0][measureIdKey] += measureValue
                , acc) : [...acc, { [measureIdKey]: measureValue }])
    }, [])) 
}))

console.log(output)

In Typescript, it has some modifications to follow the typing stuff.

type Info = {
    measureValue: string;
    measureId: number;
}

type Foo = { [key: string]: string | number; } 

type Department = {
    rowId: number;
    departmentExternalId: string;
    departmentName: string;
    measureName: string;
    measureValue: number;
} | Info


let output = Object.entries(obj).map(([key, value]) => ({
    "department": key,
    ...applyReduce(value as Info[])
})) 
 
function applyReduce(foo: Info[]) {
    return foo.reduce((acc: Info[], { measureValue, measureId }: Info): any => {
        let measureIdKey = `measure${measureId}`

        let currId: Foo[] = []  // this line is needed to compile
        acc.forEach(x => currId.push(x)) // this line too

        currId = currId.filter(x => x[measureIdKey])
        return (currId.length ?
            (currId[0][measureIdKey] += measureValue
                , acc) : [...acc, { [measureIdKey]: measureValue }])
    }, []).reduce((a, c) => ({...a,...c}), {})  // added another reduce
} 

console.log(output)
about 4 years ago · Juan Pablo Isaza Relatório

0

By using the function Array.prototype.reduce we can create an object with keys equal to the departmentNames, and then we can extract the objects as an array with the function Object.values.

Finally, by using the function Array.prototype.flatMap we can transform an array of arrays into a simple array.

const obj = {  "Account Management": [{    rowId: 1288,    departmentExternalId: "7",    departmentName: "Account Management",    measureName: "Total Headcount Expense",    measureId: 15,    measureValue: 17282  }, {    rowId: 1289,    departmentExternalId: "7",    departmentName: "Account Management",    measureName: "Salary",    measureId: 5,    measureValue: 11666  }, ],  "Client Services General (COR)": [{    rowId: 1294,    departmentExternalId: "114",    departmentName: "Client Services General (COR)",    measureName: "Total Headcount",    measureId: 2,    measureValue: 1  }, {    rowId: 1295,    departmentExternalId: "114",    departmentName: "Client Services General (COR)",    measureName: "Total Headcount",    measureId: 2,    measureValue: 100  }]},
      output = Object.values(obj).flatMap((array) => {
        return Object.values(array.reduce((a, d) => {
          const currentMeasure = a[d.departmentName]?.[`measure${d.measureId}`];

          a[d.departmentName] = {...(a[d.departmentName] || {department: d.departmentName}), [`measure${d.measureId}`]: (currentMeasure || 0) + d.measureValue};
          return a;
        }, {}));
      });

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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