我有“实体”和“实体模板”。顶级实体与“templateId1”实体模板相关联。从那里,实体通过使用其 id 与路径中的另一个实体绑定。我试图以一种有效的方式结束以下数据结构:
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
}
]
}
]
}
]
}
];
该结构的最初灵感来自 MongoDB 文档:
https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-materialized-paths/
我有一个稍微不同的用例,“实体模板”是顶级父级,但在“实体”中,用例是相同的。非常感谢任何见解。