Displayed below is an array containing categories and the corresponding solutions they belong to. Each category is unique while solutions may repeat.
const categories = [
{ category: 'Patch Leads', solution: 'Data Solutions' },
{ category: 'Cables', solution: 'Data Solutions' },
{ category: 'Nails', solution: 'Hardware' },
{ category: 'Locks', solution: 'Hardware' },
{ category: 'Screws', solution: 'Hardware' },
{ category: 'Cabinets', solution: 'Cabinet Solutions' },
{ category: 'Swing Frames', solution: 'Cabinet Solutions' },
{ category: 'Racks', solution: 'Cabinet Solutions' },
{ category: 'Fire Cables', solution: 'Fire Solutions' },
];
A new array of solution objects needs to be returned in the following format. All necessary data for this format is accessible during iteration, including index values for ids; however, there are challenges in structuring the code correctly.
const solutions = [
{
id: "0",
name: "Data Solutions",
categories: [
{
id: "0",
name: "Cables",
slug: "cables"
},
{
id: "1",
name: "Patch Leads",
slug: "patch-leads"
}
]
},
{
id: "1",
name: "Hardware",
categories: [
{
id: "0",
name: "Nails",
slug: "nails"
},
{
id: "1",
name: "Locks",
slug: "locks"
},
{
id: "2",
name: "Screws",
slug: "screws"
}
]
},
{
id: "2",
name: "Cabinet Solutions",
categories: [
{
id: "0",
name: "Cabinets",
slug: "cabinets"
},
{
id: "1",
name: "Swing Frames",
slug: "swing-frames"
},
{
id: "2",
name: "Racks",
slug: "racks"
}
]
},
{
id: "3",
name: "Fire Solutions",
categories: [
{
id: "0",
name: "Fire Cables",
slug: "fire-cables"
}
]
}
]