Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138163

Get only one parents of child in php or JavaScript

$
0
0

Here is my problem. I want to get grand parent or parent of my child. Please check below.

                                     Root 
                              A              H       
                         B         C         I                                                    
                         E       F   G     J   K

Funtion getParent(data){
    // Code is here
    return parent;
}

I tried but stuck in nested loop , I want to create in PHP or javascript that will give me only one parent like

getParent([E,F,G]) then return will be **A**
getParent([E]) then return will be **B**
getParent([A,G]) then return will be **Root**
getParent([F,G,H]) then return will be **Root**
getParent([I]) then return will be **H**
getParent([Root]) then return will be **Root**
getParent([F,G]) then return will be **C**
getParent([B,C,K]) then return will be **Root**

Please have look and resolve this problem.


Viewing all articles
Browse latest Browse all 138163

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>