const reply = [
{
"id": "499269026",
"post_id": "23869903",
"user_id": "21255192",
"parentId":0,
"quote_user_id": 0,
"content": "支持支持支持",
"view_date": "12个月",
"dated": 1624866530,
"ip": "1",
"state": "1",
"o_id": "10007",
"site": "61",
"to_site": "0",
"hongren": [
],
"step": "0",
},
{
"id": "73357689",
"post_id": "23869903",
"parentId": "499269026",
"reply_id": "499284086",
"post_user_id": "16821106",
"user_id": "23138501",
"content": "支持支持支持",
"state": "1",
"dated": "1624930776",
"user_nick": "记忆格式化",
"avatar": "http://www.qubaobei.com/files/head/300/20210625/27/16245862497994.jpg",
"pic": "",
},
{
"id": "73379332",
"post_id": "23885273",
"parentId": "499416716",
"reply_id": "499418180",
"post_user_id": "22813688",
"top_user_id": "14663119",
"user_id": "11374812",
"content": "这是管理员的QQ号 [图片]",
"state": "1",
"dated": "1625580282",
"str1": "http://www.qubaobei.com/uploadnew/2021/7/6/22/640p/21971625580281.jpg",
"avatar": "http://www.qubaobei.com/files/head/300/20210217/13/16135619876365.jpg",
"pic": "http://www.qubaobei.com/uploadnew/2021/7/6/22/640p/21971625580281.jpg",
}
]
function orderBy(list: Array<any>, key: any) {
return list.sort((a, b) => a[key] - b[key]);
}
function deepTree(list: Array<any>) {
const newList: Array<any> = [];
const map: any = {};
list.forEach((e) => (map[e.id] = e));
list.forEach((e) => {
const parent = map[e.parentId];
if (parent) {
(parent.children || (parent.children = [])).push(e);
} else {
newList.push(e);
}
});
const fn = (list: Array<any>) => {
list.map((e) => {
if (e.children) {
e.children = orderBy(e.children, "dated");
fn(e.children);
}
});
};
return orderBy(newList, "orderNum");
}
console.log(deepTree(reply))
console