Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

336
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda