I have "Entities" and "Entity Templates". The top level entity is tied to an Entity Template which is the "templateId1". From there an entity is tied to another entity in a path by using its id. I'm trying to end up with the following data structure in an efficient way:
const entities = [
{
id: 'xi32',
name: 'Some name',
path: ',templateId1,'
},
{
id: 'x382',
name: 'Some name 2',
path: ',templateId1,xi32,'
},
{
id: '2oxwo',
name: 'Some name 3',
path: ',templateId1,xi32,x382,'
},
{
id: '2',
name: '2',
path: ',templateId1,'
},
{
id: '2-2',
name: '2-2',
path: ',templateId1,2,'
},
{
id: '3-3',
name: '3-3',
path: ',templateId1,3,'
},
{
id: '3-3-3',
name: '3-3-3',
path: ',templateId1,3,3-3,'
},
{
id: '3-3-3-3',
name: '3-3-3-3',
path: ',templateId1,3,3-3,3-3-3,'
},
{
id: '3',
name: '3',
path: ',templateId1,'
}
];
const desiredResult = [
{
id: 'xi32',
name: 'Some name',
path: ',templateId1,',
children: [
{
id: 'x382',
name: 'Some name 2',
path: ',templateId1,xi32,',
children: [
{
id: '2oxwo',
name: 'Some name 3',
path: ',templateId1,xi32,x382,',
children: null
}
]
}
]
},
{
id: '2',
name: '2',
path: ',templateId1,',
children: [
{
id: '2-2',
name: '2-2',
path: ',templateId1,2,',
children: null
}
]
},
{
id: '3',
name: '3',
path: ',templateId1,',
children: [
{
id: '3-3',
name: '3-3',
path: ',templateId1,3,',
children: [
{
id: '3-3-3',
name: '3-3-3',
path: ',templateId1,3,3-3,',
children: [
{
id: '3-3-3-3',
name: '3-3-3-3',
path: ',templateId1,3,3-3,3-3-3,',
children: null
}
]
}
]
}
]
}
];
Initial inspiration for the structure came from the MongoDB documentation:
https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-materialized-paths/
I had a slightly different use case with the "Entity Template" being the top level parent, but within the "Entities" the use case is the same. Any insight is much appreciated.