Here is the issue at hand:
const data : [
{labelSlug: 'cola', category: 'catering', subCategory: 'drinks', provider: 'coca cola'},
{labelSlug: 'cola', category: 'catering', subCategory: 'drinks', provider: 'coca cola'},
{labelSlug: 'fanta', category: 'catering', subCategory: 'drinks', provider: 'coca cola'},{labelSlug: 'activities1', category: 'activities', subCategory: 'activitiesSub1', provider: 'blabla'},
{labelSlug: 'activities2', category: 'activities', subCategory: 'activitiesSub1', provider: 'blabla'},
{labelSlug: 'equipments1', category: 'equipments', subCategory: 'equipmentsSub1', provider: 'blabla'},
{labelSlug: 'equipments2', category: 'equipments', subCategory: 'equipmentsSub2', provider: 'blabla'}
{labelSlug: 'cola', category: 'catering', subCategory: 'drinks', provider: 'coca cola'},
]
I am attempting to achieve a structure like this for each category and subCategory:
{
catering : [
drinks : [
{ labelSlug: cola, provider: coca cola, count: 3 },
{ labelSlug: fanta, provider: coca cola, count: 1 }
]
]
}
The challenge lies in figuring out how to configure this setup, I have exhausted all possible approaches:
const array = data.map((elem) => {
const item = {};
const sub = elem.subCategory;
const cat = elem.category;
// i've tried many things like that just to get everything in the right place (I didnt try to get the count part yet)
// item[cat[sub]].labelSlug = elem.labelSlug;
// item[cat][sub].labelSlug = elem.labelSlug;
// item[cat[sub.labelSlug]] = elem.labelSlug;
// const item = {
// [`${cat}`] : {
// [`${sub}`] : {
// labelSlug: elem.labelSlug
// }
// }
// }
return item;
})