簡體   English   中英

處理數據並記錄誰來自請求

[英]Processing data and recording who came from the request

我有一張桌子:

  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到父注釋對象的映射。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM