使用 php 或 javascript 排序评论数据算法
sorting comments data algorithm with php or javascript
const comments = [
{ 'id': 1, 'origin_id': 0 },
{ 'id': 2, 'origin_id': 0 },
{ 'id': 3, 'origin_id': 2 },
{ 'id': 4, 'origin_id': 2 },
{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },
{ 'id': 7, 'origin_id': 0 },
{ 'id': 8, 'origin_id': 0 },
{ 'id': 9, 'origin_id': 0 },
{ 'id': 10, 'origin_id': 0 },
];
// or? data structure somthing like this? i dont konw how to use such order and depth.
const comments = [
{ 'id': 1, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 2, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 3, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 4, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 5, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 6, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 7, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 8, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 9, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 10, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
];
我想要一些算法,比如评论中的评论。
只有那么多的数据,这甚至可能吗?
如果有人知道实现该目标的更好方法,请告诉我。
我想了解一下。任何代码语言对我来说都可以。
但我更喜欢 PHP 和 JS.
const comments = [
{ 'id': 1, 'origin_id': 0 },
{ 'id': 2, 'origin_id': 0 },
{ 'id': 3, 'origin_id': 2 },
{ 'id': 4, 'origin_id': 2 },
{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },
];
// result i expected;
const result_comments = [
{ 'id': 1, 'origin_id': 0 },
{
'id': 2, 'origin_id': 0,
'children': [{
'id': 3, 'origin_id': 2, 'children': [{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },]
}, { 'id': 4, 'origin_id': 2 }]
},
];
这里的嵌套object基本上就像一棵树,评论变成了树节点。
首先,用 children 的 属性 创建一个字典,然后从中分配 children。
评论 ID 是按升序创建的,因此创建后无需 re-sort。
// assuming that id are sorted, else you have to sort comments by id.
const comments = [
{ 'id': 1, 'origin_id': 0 },
{ 'id': 2, 'origin_id': 0 },
{ 'id': 3, 'origin_id': 2 },
{ 'id': 4, 'origin_id': 2 },
{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },
];
// creating dictinary where key is id and value is object with added children property
let dic = comments.reduce((acc, obj) => (acc[obj.id] = {...obj, children : [] }, acc), {});
// initial set
let treeRoot = {'id': 0, 'origin_id': 0 , children : []};
// adding in dictionary so i can add origin_id 0 to tree root
dic[treeRoot.id] = treeRoot;
// populating the childrens
comments.forEach(obj => dic[obj.origin_id].children.push(dic[obj.id]));
// if you want the array that inside treeRoot.children
console.log(treeRoot);
const comments = [
{ 'id': 1, 'origin_id': 0 },
{ 'id': 2, 'origin_id': 0 },
{ 'id': 3, 'origin_id': 2 },
{ 'id': 4, 'origin_id': 2 },
{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },
{ 'id': 7, 'origin_id': 0 },
{ 'id': 8, 'origin_id': 0 },
{ 'id': 9, 'origin_id': 0 },
{ 'id': 10, 'origin_id': 0 },
];
// or? data structure somthing like this? i dont konw how to use such order and depth.
const comments = [
{ 'id': 1, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 2, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 3, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 4, 'origin_id': 2, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 5, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 6, 'origin_id': 3, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 7, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 8, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 9, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
{ 'id': 10, 'origin_id': 0, 'reply_order': 0, 'reply_depth': 0 },
];
我想要一些算法,比如评论中的评论。 只有那么多的数据,这甚至可能吗?
如果有人知道实现该目标的更好方法,请告诉我。
我想了解一下。任何代码语言对我来说都可以。 但我更喜欢 PHP 和 JS.
const comments = [
{ 'id': 1, 'origin_id': 0 },
{ 'id': 2, 'origin_id': 0 },
{ 'id': 3, 'origin_id': 2 },
{ 'id': 4, 'origin_id': 2 },
{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },
];
// result i expected;
const result_comments = [
{ 'id': 1, 'origin_id': 0 },
{
'id': 2, 'origin_id': 0,
'children': [{
'id': 3, 'origin_id': 2, 'children': [{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },]
}, { 'id': 4, 'origin_id': 2 }]
},
];
这里的嵌套object基本上就像一棵树,评论变成了树节点。 首先,用 children 的 属性 创建一个字典,然后从中分配 children。
评论 ID 是按升序创建的,因此创建后无需 re-sort。
// assuming that id are sorted, else you have to sort comments by id.
const comments = [
{ 'id': 1, 'origin_id': 0 },
{ 'id': 2, 'origin_id': 0 },
{ 'id': 3, 'origin_id': 2 },
{ 'id': 4, 'origin_id': 2 },
{ 'id': 5, 'origin_id': 3 },
{ 'id': 6, 'origin_id': 3 },
];
// creating dictinary where key is id and value is object with added children property
let dic = comments.reduce((acc, obj) => (acc[obj.id] = {...obj, children : [] }, acc), {});
// initial set
let treeRoot = {'id': 0, 'origin_id': 0 , children : []};
// adding in dictionary so i can add origin_id 0 to tree root
dic[treeRoot.id] = treeRoot;
// populating the childrens
comments.forEach(obj => dic[obj.origin_id].children.push(dic[obj.id]));
// if you want the array that inside treeRoot.children
console.log(treeRoot);