我有一张桌子:
comment_id | user_Id | product_id | parent_id | reply_id | ... |
--------------------------------------------------------------------
1 | 20 | 1 | null | null | ... |
2 | 20 | 1 | null | null | ... |
3 | 7 | 1 | 1 | 1 | ... |
4 | 7 | 1 | 1 | 2 | ... |
5 | 7 | 1 | null | null | ... |
6 | 7 | 1 | null | null | ... |
7 | 7 | 1 | 2 | 2 | ... |
我收到了请求的回复:
{
"comment_id": 1,
"user_id": 20,
"product_id": 1,
"parent_id": null,
"reply_id": null,
...
},
{
"comment_id": 2,
"user_id": 20,
"product_id": 1,
"parent_id": null,
"reply_id": null,
...
},
{
"comment_id": 3,
"user_id": 7,
"product_id": 1,
"parent_id": "1",
"reply_id": "1",
...
},
{
"comment_id": 4,
"user_id": 7,
"product_id": 1,
"parent_id": "1",
"reply_id": "2",
...
},
{
"comment_id": 5,
"user_id": 7,
"product_id": 1,
"parent_id": null,
"reply_id": null,
...
},
{
"comment_id": 6,
"user_id": 7,
"product_id": 1,
"parent_id": null,
"reply_id": null,
...
},
{
"comment_id": 7,
"user_id": 7,
"product_id": 1,
"parent_id": "2",
"reply_id": "2",
...
}
我需要以这种格式输出它:
{
{
"comment_id": 1,
"user_id": 20,
"product_id": 1,
"parent_id": null,
"reply_id": null,
...,
"nested_comments": [
{
"comment_id": 3,
"user_id": 7,
"product_id": 1,
"parent_id": "1",
"reply_id": "1",
...
},
{
"comment_id": 4,
"user_id": 7,
"product_id": 1,
"parent_id": "1",
"reply_id": "2",
...
}
]
},
{
"comment_id": 2,
"user_id": 20,
"product_id": 1,
"parent_id": null,
"reply_id": null,
"user_name": "Nikita Velichkin",
...,
"nested_comments": [
{
"comment_id": 7,
"user_id": 7,
"product_id": 1,
"parent_id": "2",
"reply_id": "2",
...
}
]
},
{
"comment_id": 5,
"user_id": 7,
"product_id": 1,
"parent_id": null,
"reply_id": null,
...,
"nested_comments": []
},
{
"comment_id": 6,
"user_id": 7,
"product_id": 1,
"parent_id": null,
"reply_id": null,
...,
"nested_comments": []
},
}
如果该行的comment_id
和parent_id
字段包含相同的值,请将此行写入nested_comments
。
也就是说,对于父注释,字段parent_id
和reply_id
将为空;对于响应广播评论的评论,它们以nested_comments
编写。
我应该怎么做:
let comments = data_comments.rows; // I write down the answer from the server
for (let i = 0; i < comments.length; i++) {
comments[i].nested_comments = []; // where to write the creation of fields
if (comments[i].comment_id === comments[i].parent_id) { //field alignment
comments[i].nested_comments.push(comments[i]); // field entry
}
}
console.log(comments)
我将所有内容都放在一个数组中,但是我需要标识父级,并在nested_comments
字段中输入该父级的子级命令
答案 0 :(得分:2)
在此代码中,我假设每个父注释都在其嵌套注释之前。依时间顺序排列。
let comments =
[{
"comment_id": 1,
"user_id": 20,
"product_id": 1,
"parent_id": null,
"reply_id": null,
},
{
"comment_id": 2,
"user_id": 20,
"product_id": 1,
"parent_id": null,
"reply_id": null,
},
{
"comment_id": 3,
"user_id": 7,
"product_id": 1,
"parent_id": "1",
"reply_id": "1",
},
{
"comment_id": 4,
"user_id": 7,
"product_id": 1,
"parent_id": "1",
"reply_id": "2",
},
{
"comment_id": 5,
"user_id": 7,
"product_id": 1,
"parent_id": null,
"reply_id": null,
},
{
"comment_id": 6,
"user_id": 7,
"product_id": 1,
"parent_id": null,
"reply_id": null,
},
{
"comment_id": 7,
"user_id": 7,
"product_id": 1,
"parent_id": "2",
"reply_id": "2",
}];
let newArr=[], tmp = {};
for (let c of comments) {
if (c.parent_id === null) {
newArr.push(c);
tmp[c.comment_id] = c;
}
else {
tmp[c.parent_id].nested_comments = tmp[c.parent_id].nested_comments || []; // only create nested_comments if replys exist
tmp[c.parent_id].nested_comments.push(c);
}
}
console.log(newArr);
tmp
用作从comment_id
到父注释对象的映射。