I'm trying to build an array of objects from another two arrays, can someone help me out.
const parent =[
{ Id: 1, Cate: 'Accommodation', p_id: null },
{ Id: 4, Cate: 'National Travel', p_id: null }
]
const child =[
{ Id: 2, Cate: 'Hotel Accommodation', p_id: 1 },
{ Id: 3, Cate: 'Own arrangement', p_id: 1 },
{ Id: 5, Cate: 'Air', p_id: 4 },
{ Id: 6, Cate: 'Bus', p_id: 4 },
{ Id: 7, Cate: 'AC Volvo', p_id: 6 },
{ Id: 8, Cate: 'Luxury Bus', p_id: 6 },
{ Id: 9, Cate: 'Bus', p_id: 6 }
]
const data = [
{
id: 1,
tittle: 'Accommodation',
subItem: [
{ id: 2, tittle: 'Hotel Accommodation', subItems: [] },
{ id: 3, tittle: 'Own arrangement', subItems: [] },
],
},
{
id: 4,
tittle: 'National Travel',
subItem: [
{ id: 5, tittle: 'Air', subItems: [] },
{
id: 6,
tittle: 'Bus',
subItem: [
{ id: 7, tittle: 'AC Volvo' },
{ id: 5, tittle: 'Air' },
{ id: 8, tittle: 'Luxury Bus' },
{ id: 9, tittle: 'Bus' },
],
},
],
},
];
parent
and child
are the data I get from DB, now I need to change that into something looking like data
. The thing is it has to be dynamic, as subItems
can go long as it needs to be, how can I make something like that?