I have the following JavaScript object:
this is basically a list of comments, the actual list is much longer and more deeply nested, so this is a simplified version.
[
{
id: 200,
comment: "If you have any question, please comment here.",
depth: 1,
parent_id: null,
comments: [],
},
{
id: 201,
comment: "Hi, I'm stuck at the installation of node.js. Can I ask help?",
depth: 2,
parent_id: 200,
comments: [],
},
{
id: 202,
comment: "This is a detailed explanation, great.",
depth: 1,
parent_id: null,
comments: [],
},
{
id: 203,
comment: "Hello! That's weird. Do you have a screenshot?",
depth: 3,
parent_id: 201,
comments: [],
},
]
I want to convert the above to the following. But I think I use .map or similar, but I don't have a clear idea of how to implement this.
[
{
id: 200,
comment: "If you have any question, please comment here.",
depth: 1,
parent_id: null,
comments: [
{
id: 201,
comment: "Hi, I'm stuck at the installation of node.js. Can I ask help?",
depth: 2,
parent_id: 200,
comments: [
{
id: 203,
comment: "Hello! That's weird. Do you have a screenshot?",
depth: 3,
parent_id: 201,
comments: [],
},
],
},
],
},
{
id: 202,
comment: "This is a detailed explanation, great.",
depth: 1,
parent_id: null,
comments: [],
},
]
Thanks.