我有一批评论,他们的格式类似
[
{
commentID:1,
comment_parentID:0
},
{
commentID:2,
comment_parentID:1
},
{
commentID:3,
comment_parentID:0
},
{
commentID:4,
comment_parentID:2
},
{
commentID:5,
comment_parentID:1
}
]
所有层级的评论都被平铺开来为一层,现在我要变为多层的
[
{
commentID:1,
comments:[
{
commentID:2,
comment_parentID:1,
comments:[
{
commentID:4,
comment_parentID:2
},
]
},
{
commentID:5,
comment_parentID:1
}
]
},
{
commentID:3,
comment_parentID:0
}
]
如何循环才能最快,他们的顺序不一定是正序到倒叙,是有可能乱的. ID 是唯一的.