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

234
Vistas
How to filter an array of objects by nested keys and values

What I've got is an array of objects, like so

const data = [
{
    group: "Group 1",
    category: "G1 Cat 1",
    value: 50,
    budgetid: "53780a3b-ad3a"
},
{
    group: "Group 1",
    category: "G1 Cat 1",
    value: 11,
    budgetid: "53780a3b-ad3a"
},
{
    group: "Group 1",
    category: "G1 Cat 1",
    value: 50,
    budgetid: "eece3355-921e"
},
{
    group: "Group 1",
    category: "G1 Cat 1",
    budgetid: "1a593f25-28ee",
    value: 25
},
{
    group: "Group 1",
    category: "G1 Cat 2",
    value: 5,
    budgetid: "53780a3b-ad3a"
},
{
    group: "Group 1",
    category: "G1 Cat 2",
    value: 50,
    budgetid: "eece3355-921e"
},
{
    group: "Group 1",
    category: "G1 Cat 3",
    budgetid: "1a593f25-28ee",
    value: 25
},
{
    group: "Group 1",
    category: "G1 Cat 3",
    budgetid: "1a593f25-28ee",
    value: 1
},
{
    group: "Group 2",
    category: "G2 Cat 1",
    value: 50,
    budgetid: "53780a3b-ad3a"
},
{
    group: "Group 2",
    category: "G2 Cat 1",
    value: 5,
    budgetid: "53780a3b-ad3a"
},
{
    group: "Group 2",
    category: "G2 Cat 2",
    value: 28,
    budgetid: "eece3355-921e"
}

];

What I want to create is an object looking like this:

    const newSet = {
     rows: [
     { 
      group: 'Group 1',
      categories: [
       { 
        category: 'G1 Cat 1',
        monthlyValues: [{'53780a3b-ad3a': 61}, {'eece3355-921e': 50}, {'1a593f25-28ee': 25}]      
       },
       { 
        category: 'G1 Cat 2',
        monthlyValues: [{'53780a3b-ad3a': 5}, {'eece3355-921e': 50}, {'1a593f25-28ee': 26}]      
        }
       ]
        },
        { 
      group: 'Group 2',
      categories: [
       { 
         category: 'G2 Cat 1',
         monthlyValues: [{'53780a3b-ad3a': 55}]      
       },
       { 
         category: 'G2 Cat 2',
         monthlyValues: [{'eece3355-921e': 28}]      
       },
      ]
    }
 ]
}

So the data is grouped by its groups, then each group has categories that are unique and finally each category has a monthly total value which is sum of the values for the same budgetid in each group/category. What is also important is that the key for the total values needs to be data.budgetid and in string format as it contains hyphens and numbers.

about 4 years ago · Juan Pablo Isaza
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