I have a JSON data representing a multi-level comment system as shown below:
[{
"thread_id": 2710,
"parent_id": "",
"username": "string",
"comment": "string",
"postdate": "2017-06-09T07:12:32.000Z",
"id": 1
}, {
"thread_id": 2710,
"parent_id": "1",
"username": "string2",
"comment": "string2",
"postdate": "2017-06-09T07:12:32.000Z",
"id": 2
}, {
"thread_id": 2710,
"parent_id": "",
"username": "string32",
"comment": "strin2g2",
"postdate": "2017-06-09T07:12:32.000Z",
"id": 3
}, {
"thread_id": 2710,
"parent_id": "",
"username": "str23ing32",
"comment": "strrgein2g2",
"postdate": "2017-06-09T07:12:32.000Z",
"id": 4
}, {
"thread_id": 2710,
"parent_id": "3",
"username": "str2rr3ing32",
"comment": "strr@gein2g2",
"postdate": "2017-06-09T07:12:32.000Z",
"id": 5
}, {
"thread_id": 2710,
"parent_id": "3",
"username": "str2ergergrr3ing32",
"comment": "strr@geinergerg2g2",
"postdate": "2017-06-09T07:12:32.000Z",
"id": 6
}, {
"thread_id": 2710,
"parent_id": "6",
"username": "str2ergrrrgergergrr3ing32",
"comment": "strr@geiergergernergerg2g2",
"postdate": "2017-06-09T07:12:32.000Z",
"id": 7
}]
How can I create a javascript function to restructure the Response as shown below?
{ id: 1
parent_id: "",
....,
comments: [
{
id: 12,
parent_id: 1,
comments: [{ parent_id: 12 ....}]
}
]