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

88
Views
I want to transform all the data in my JavaScript nested array having the same name and then group them together

I'm trying to group the data in my nested array that has same categoryName together, then transform them into an object. The code snippet below shows what my data looks like. .......................................................................................................................................................................................................

here is what the data looks like

data = [
    {
        name: "listName",
        id:434343,
        data: [
            {
                id:434343,
                categoryName: school, 

            }, 
            {
                id:234343,
                categoryName: house, 
                
            },
            {
                id:2000,
                categoryName: school, 
                
            },
            {
                id:2333,
                categoryName: house, 
                
            }
        ]
    }, 
    {
        name: "anotherListName",
        id:434343,
        data: [
            {
                id:434343,
                categoryName: school, 

            }, 
            {
                id:234343,
                categoryName: house, 
                
            },
            {
                id:2000,
                categoryName: school, 
                
            },
            {
                id:2333,
                categoryName: house, 
                
            }
        ]
    }
]

This is what the transformed data should look like

transformed = [
    {
        name: "listName",
        id:43453,
        data: {
            school: [
               {
                id:434343,
                categoryName:school   
               }, 
               {
                id:2000,
                categoryName:school   
               }, 
            ],
            house: [
               {
                id:234343,
                categoryName:house   
               }, 
               {
                id:2333,
                categoryName:house   
               }, 
            ],
        } 
    },
    {
        name: "listName",
        id:43453,
        data: {
            school: [
               {
                id:434343,
                categoryName:school   
               }, 
               {
                id:2000,
                categoryName:school   
               }, 
            ],
            house: [
               {
                id:234343,
                categoryName:house   
               }, 
               {
                id:2333,
                categoryName:house   
               }, 
            ],
        } 
    },
]

this is my code

const transformed = []
list.forEach(d => {
    d.data.forEach(item => {
        const exist = transformed.find(t => t.categoryName == item.name)
        if(exist) 
           ///got confused at this point
        else 
            transformed.push(item)
    })
})
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!