Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!