i really struggle with array.reduce() and i think in this case i'm not sure if i've got the right approach. Typically i have a starting array and i know what i need to end up with but i can't seem to get the grouping right.
This is the starting array
[
{ name: 'Home' },
{
name: 'Services',
menu: [
{ name: 'Painting' },
{ name: 'Decorating' },
{ name: 'Lawn mowing', submenu: 'Garden' },
{ name: 'Tree surgery', submenu: 'Garden' },
{ name: 'Edging', submenu: 'Garden' }
]
},
{ name: 'Contact' }
]
and what i'd like to end up with is this
[
{ name: 'Home' },
{
name: 'Services',
menu: [
{ name: 'Painting' },
{ name: 'Decorating' },
{
name: 'Garden',
menu: [
{ name: 'Lawn mowing', submenu: 'Garden' },
{ name: 'Tree surgery', submenu: 'Garden' },
{ name: 'Edging', submenu: 'Garden' }
]
}
]
},
{ name: 'Contact' }
]
So i'd like to be able to group by anything that has a submenu and then return a new sorted array.