我有一个包含子字段的“页面”对象列表。这个子字段引用列表中的另一个对象。我想根据这个字段从这个列表创建一个树形层次结构。 我已经找到了解决办法here,但如果我有一个父field.Here是我原来的名单是什么样子它只能:构建带有子字段的平面列表的层次结构树?
[
{
id: 1,
title: 'home',
child: null
},
{
id: 2,
title: 'about',
child: null
},
{
id: 3,
title: 'team',
child: 4
},
{
id: 4,
title: 'company',
child: 2
}
]
我想将它转化成一个树状结构是这样的:
[
{
id: 1,
title: 'home',
},
{
id: 3,
title: 'team',
children: [
{
id: 4,
title: 'company',
children: {
id: 2,
title: 'about',
}
}
]
]
我希望可以随时调用任意列表的可重用函数。任何人都知道处理这个问题的好方法吗?任何帮助或建议将不胜感激!