0

I am writing a commenting system. For a particular page, I am fetching the the comments as highly nested JSONs (a comment contains its children, that in turn may contain other children). I want to parse and display these comments in a threaded manner. Here's an example of one such JSON:

{
  "children": [
    {
      "children": [
        {
          "children": [],
          "content": "Here is a child8",
          "id": "child8",
          "pageURIHash": "3988665684",
          "parentId": null
        }
      ],
      "content": "Here is child1",
      "id": "child1",
      "pageURIHash": "3988665684",
      "parentId": null
    },
    {
      "children": [
        {
          "children": [],
          "content": "Here is child3",
          "id": "child3",
          "pageURIHash": "3988665684",
          "parentId": null
        },
        {
          "children": [],
          "content": "Here is child4",
          "id": "child4",
          "pageURIHash": "3988665684",
          "parentId": null
        },
        {
          "children": [],
          "content": "Here is a child5",
          "id": "child5",
          "pageURIHash": "3988665684",
          "parentId": null
        },
        {
          "children": [
            {
              "children": [
                {
                  "children": [],
                  "content": "Here is a child9",
                  "id": "child9",
                  "pageURIHash": "3988665684",
                  "parentId": null
                },
                {
                  "children": [],
                  "content": "Here is a child10",
                  "id": "child10",
                  "pageURIHash": "3988665684",
                  "parentId": null
                }
              ],
              "content": "Here is a child7",
              "id": "child7",
              "pageURIHash": "3988665684",
              "parentId": null
            }
          ],
          "content": "Here is a child6",
          "id": "child6",
          "pageURIHash": "3988665684",
          "parentId": null
        }
      ],
      "content": "Here is child2",
      "id": "child2",
      "pageURIHash": "3988665684",
      "parentId": null
    }
  ],
  "content": "Here is a parent comment",
  "id": "parent",
  "pageURIHash": "3988665684",
  "parentId": null
}

Edit: Ignore the null parentIds. This is just a mock document. Each child comment will have a corresponding parentId, except for the top-level comment

What would be the strategy to solve this problem? I can parse the JSON but how would I display the comments in a threaded fashion? Sorry if this question appears naive but I haven't worked on the front-end much.

Thanks a lot!

Edit: I am using JQuery and Bootstrap. Here's an example of how I want the formatting to be:

https://www.reddit.com/r/AskReddit/comments/a9jcxo/what_are_some_nonobvious_early_signs_that_a/

Edit: Looks like I found a solution. I am using jsTree and it seems to work fine for my use-case.

2
  • Which part(key) is comment here ? What is your desired output format ? Will array of comments work for you ? Commented Dec 26, 2018 at 15:43
  • id: parent is the root comment. It has two children: child1 and child2. The output formatting that I want is like: reddit.com/r/AskReddit/comments/a9jcxo/… I need to show the relationship b/w parent and children comments in the UI, just like Reddit. Commented Dec 26, 2018 at 15:51

2 Answers 2

1

Your question is a bit vague. Can you provide a sample of what you expect the output to be from the document you have shown in the question?

Edit: From what I could understand, this document is the data structure that you use to store the "comments" on a webpage. The content in the document is the comment and you wish to show these in a tree like structure.

I would try recursion to flatten out the data with information like which level it is in and then use that to show the comments on the page.

eg.

let flattenedData = []; 

function flattenify(doc, level) {
  if (!level) {
    level = 0;
  }
  if (!doc || !doc.children || doc.children.length === 0) {
    return;
  }

  for (let index = 0; index < doc.children.length; index++) {
    let node = doc.children[index];
    flattenedData.push({
      content: node.content,
      id: node.id,
      pageURIHash: node.pageURIHash,
      parentId: node.parentId,
      level: level,
    });

    flattenify(node, (level + 1));
  }
}

flattenify(commentDoc); // the document that contains the json structure

console.log(flattenedData);

I have not run this code, so I do not guarantee that this will run successfully on your document. But, this should give you an idea about how to walk over it. Use the level information to push spaces for indentations before the comment.

Sign up to request clarification or add additional context in comments.

2 Comments

Take Reddit for example. When we load a Reddit page, it loads all the comments and displays them as threaded comments, right? The exact same way. For example: reddit.com/r/Sneakers/comments/8bbc45/alphabounce_or_ultraboost
Here's another example containing nested comments: reddit.com/r/AskReddit/comments/a9jcxo/…
0

provide the frontend technology you are using. Is it angular, react, jQuery or something else? You might also want to specify more context if you need a more targeted answer.

EDIT

Since you are using JQuery, you could possibly use accordions and modify it to show the threads within each comment. Refer Accordion in the JQuery documentation.

4 Comments

I am using Bootstrap and JQuery.
Here is an example of how I want the formatting to be: reddit.com/r/AskReddit/comments/a9jcxo/…
This is not an answer and the question is framework agnostic
I couldn't post a comment, hence the answer with a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.