0

I have a 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    | ... |

I receive a response from the request:

{
    "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",
    ...
}

I need to output it in this format:

{
    {
        "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": []
    },
}

If the line contains the same values for the comment_id andparent_id fields, write this line to nested_comments.

That is, for the parent comment, the fields parent_id and reply_id will be empty; for comments that respond to the radio comment, they are written in nested_comments.

How do i suppose to do it:

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)

I have everything in one array, but I need to identify the parent and to it, in the nested_comments field, enter the child commands of this parent

MegaRoks
  • 898
  • 2
  • 13
  • 30
  • Possible duplicate of [Build tree array from flat array in javascript](https://stackoverflow.com/questions/18017869/build-tree-array-from-flat-array-in-javascript) and [How to create nested group of properties](https://stackoverflow.com/questions/47130242) and [How can I convert normal array object to multilevel array object on the javascript?](https://stackoverflow.com/questions/49424109) – adiga Feb 28 '19 at 12:30
  • What is the correlation between `parent_id` and `reply_id`? (in terms of hierarchy). Also - how many levels of hierarchy would you like to end with? – ymz Feb 28 '19 at 12:31
  • In `reply_id`, there is a` comment_id` which is answered, and `parent_id` specifies which parent they will pass to. One level of nesting will be. – MegaRoks Feb 28 '19 at 12:37

1 Answers1

2

In this code I assume that every parent comment comes before its nested comments. Which makes sence chronologically.

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 is used as a map from comment_id to the parent comment object.

Dmitry
  • 6,716
  • 14
  • 37
  • 39