Seeking a solution to retrieve interconnected records based on a parent column, where the relation can be one or many on both ends.
After attempting a recursive function without success, I found my code became overly complex and ineffective. Is there a standard method to approach this issue?
Here is a sample dataset:
id parent_id
Record1 main
RecordA1 Record1
RecordA2 Record1
RecordB1 RecordA1
RecordC1 RecordB1
This was my initial attempt at the code:
data.first_parent_id = main_parent_id;
data.categories = [];
function getCategories(parent_id) {
// -> fetch data with columns matching parent_id from input parameter
data.categories.push({
id: id,
parent_id: gr.getValue('parent_id')
});
return data.categories;
}
getCategories(data.first_parent_id);
The desired output is an object array structured like this:
obj = {
id: record1,
children: [
{
id: RecordA1,
children: [
id: RecordB1,
children: [
id: RecordC1,
children: [
]
]
]
},
{
id: RecordA2,
children: []
},
{
id: value,
children: []
}
]
};
Any advice or suggestions are greatly appreciated.
Thank you!