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

241
Visualizações
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 à 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