0

I am trying to create a table from a data structure like grapho.

I have the following tree structure

{
    "category": {
        "name": "Main Category",
        "items": []
    },
    "childs": [
        {
            "category": {
                "name": "First Partial",
                "items": []
            },
            "childs": [
                {
                    "category": {
                        "name": "Activities",
                        "items": []
                    },
                    "childs": [
                        {
                            "category": {
                                "name": "Online",
                                "items": [
                                    {"name": "Activity 1"},
                                    {"name": "Activity 2"}
                                ]
                            },
                            "category": {
                                "name": "Class",
                                "items": [
                                    {"name": "Activity 3"},
                                    {"name": "Activity 4"}
                                ]
                            }
                        }
                    ]
                },
                {
                    "category": {
                        "name": "Quizzes",
                        "items": [
                            {"name": "Quiz 1"},
                            {"name": "Quiz 2"}
                        ]
                    },
                    "childs": []
                }
            ]
        },
        {
            "category": {
                "name": "Second Partial",
                "items": []
            },
            "childs": [
                {
                    "category": {
                        "name": "Activities",
                        "items": []
                    },
                    "childs": [
                        {
                            "category": {
                                "name": "Online",
                                "items": [
                                    {"name": "Activity 5"},
                                    {"name": "Activity 6"}
                                ]
                            },
                            "category": {
                                "name": "Class",
                                "items": [
                                    {"name": "Activity 7"},
                                    {"name": "Activity 8"}
                                ]
                            }
                        }
                    ]
                },
                {
                    "category": {
                        "name": "Quizzes",
                        "items": [
                            {"name": "Quiz 3"},
                            {"name": "Quiz 4"}
                        ]
                    },
                    "childs": []
                }
            ]
        },
    ]
}

And I have to print it in a table as follows: enter image description here

What I need is some idea to walk the tree with JS to print it like this.

It is worth mentioning that the tree configuration may vary depending on the course, but the solution must still be valid.

In some cases, only the main category and pure items can be found.

Please, any ideas that help me find my way, I will thank you.

Raul Cruz
  • 37
  • 1
  • 5
  • I think it is a little more complicated than that, since the information that is needed in the first nodes is known until the end of the tree, it is a little more complex. It's not just about running the object. But thanks – Raul Cruz Feb 19 '21 at 08:00
  • 1
    You need to do it recursively. – MrUpsidown Feb 19 '21 at 08:17

0 Answers0