Skip to content
Advertisement

How do I group data that is nested in an array?

I have a array of objects and then inside each of the object I have another array, I want to group the data inside the object array according to their category name. I tried doing it with a reduce function but it is basically giving me the same data(it is not even transformed), It’s like it is not even calling.

My Data

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, 
                
            }
        ]
    }
]

my code

list.forEach(d =>
    d.data.reduce((acc, currrent) => {
      (acc[currrent.categoryName] = acc[currrent.categoryName] || []).push(
        currrent
      )
      return acc
    }, {})
  )

How I want the data to be transformed

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   
               }, 
            ],
        } 
    },
]

Advertisement

Answer

you can do something like this using map and reduce

const transform = data => data.map(({name, id, data}) => {
  return {
    name,
    id,
    data: data.reduce((res, {id, categoryName}) => {
      return {
        ...res,
        [categoryName]: [...(res[categoryName] || []), {id, categoryName }]
      }
    }, {})
  }
})



const 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',

      }
    ]
  }
]

const result = transform(data)


console.log(result)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement