0

I have a multi level nested JSON object as shown below. I am trying to find the parental hierarchy up until the root for a given key value. What will be the best way to achieve this.

Sample scenario: If I search for "Grand Child 111", I should get all the parents of "Grand Child 111" i.e. Child 11, Parent 1. The existing solution on stack overflow return sibling of the direct parent as well which isn't needed. I need only the direct parent object and grand parent object just not the their names to be retrieved.

I have tried the other solution in my previous question. It didn't work.

[{
        "name": "Parent 1",
        "children": [{
                "name": "Child 11",
                "children": [{
                    "name": "Grand child 111",
                    "children": []
                }]
            },
            {
                "name": "Child 12",
                "children": [{
                    "name": "Grand child 121",
                    "children": []
                }]
            },
            {
                "name": "Child 2",
                "children": [{
                        "name": "Grand child 21",
                        "children": []
                    }

                ]
            }
        ]
    }, {
        "name": "Parent 2",
        "children": [{
                "name": "Child 21",
                "children": [{
                        "name": "Grand child 21",
                        "children": []
                    }

                ]
            }
        ]
    }

]

Please find the jsfiddle link below

jsfiddle.net/jef9ctxz/1

I am stuck at with the array of parent node names however I would need the entire parent object. Expected result :

[{
    "name": "Parent 1",
    "children": [{
        "name": "Child 11",
        "children": [{
            "name": "Grand child 111",
            "children": []
        }]
    }]
}]

@JaromandaX I have edited the question with my solution

  • What have you tried so far? – uiTeam324 Apr 06 '20 at 05:52
  • *I have tried the other solution* - show what you've tried then – Jaromanda X Apr 06 '20 at 05:52
  • *I should get all the parents of "Grand Child 111" i.e. Child 11, Parent 11* ... there is no Parent 11 – Jaromanda X Apr 06 '20 at 06:00
  • Sorry it should be Parent 1. – Anitha Karthik Apr 06 '20 at 06:15
  • https://stackoverflow.com/questions/49401319/find-the-parent-of-a-javascript-tree-object-array I have tried the solution quoted here. It doesn't work as expected. I have also tried solution stated here https://stackoverflow.com/questions/20052900/find-all-the-parents-of-each-element It returns an array of parent nodes. However I would need an array of obj of parent nodes – Anitha Karthik Apr 06 '20 at 06:18
  • that solution won't work directly with your code - please show how you've tried that solution – Jaromanda X Apr 06 '20 at 06:20
  • https://jsfiddle.net/jef9ctxz/1/ I am stuck at with the array of parent node names however I would need the entire parent object. Expected result : [{ "name": "Parent 1", "children": [{ "name": "Child 11", "children": [{ "name": "Grand child 111", "children": [] }] }] }] – Anitha Karthik Apr 06 '20 at 06:30
  • ^ would you copy your comment and paste it into your question as an update? You can then delete your remark and ping @JaromandaX once you have done so? Thanks. – halfer Apr 06 '20 at 09:39

0 Answers0