javascript - 处理数据并记录来自请求的人

标签 javascript node.js

我有一张 table :

  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_idparent_id 字段包含相同的值,请将此行写入 nested_comments

也就是说,对于父评论,字段 parent_idreply_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 字段中输入该父级的子命令

最佳答案

在此代码中,我假设每个父注释都位于其嵌套注释之前。这按时间顺序是有意义的。

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 到父评论对象的映射。

关于javascript - 处理数据并记录来自请求的人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54925709/

相关文章:

JavaScript 对象子类

javascript - Node.JS:为什么嵌套的 Promise then 似乎乱序了?

javascript - JSON合并(更新)|过滤器|增量ID

node.js - 使用 nodejs-mongodb 驱动程序在哪里指定 "noCursorTimeout"选项?

javascript - 如何使用jquery获取元素的title属性并将其插入到元素后面?

javascript - Bootstrap 图标未显示在 Internet Explorer 中

javascript - 数组中的函数在js中为空

javascript - 在没有 Oracle Instant Client 的情况下将 Node.js 与 Oracle 配对

javascript - 异步函数返回 "undefined"

node.js - 我如何知道 Mongodb 服务器正在运行 --auth on ?