 '&nbsp; &nbsp; &nbsp;';
if(rpid > 1) {
    nm = nest+rnm+'<br>';
}
else {nm = rnm+'<br>';}

output+='' + nm + '';

But this does only one level of parent items.

A:

Since your output is not formatted, I am first creating a hierarchy of parent-child which is then being used to print the output. Instead of directly storing the values in string, I am using JSON as it's much easier to dynamically add child there.
var data = [{"id":1,"lvl":0,"pid":0,"pos":0,"nm":"HomeTree"},
    {"id":5,"lvl":1,"pid":1,"pos":0,"nm":"Books"},
    {"id":2,"lvl":1,"pid":1,"pos":1,"nm":"Jobs"},
    {"id":16,"lvl":1,"pid":1,"pos":2,"nm":"Music"},
    {"id":17,"lvl":2,"pid":16,"pos":0,"nm":"Relax Sets"},
    {"id":12,"lvl":1,"pid":1,"pos":3,"nm":"Cars"},
    {"id":13,"lvl":2,"pid":12,"pos":0,"nm":"BMW"},
    {"id":14,"lvl":1,"pid":1,"pos":4,"nm":"Web"},
    {"id":15,"lvl":2,"pid":14,"pos":0,"nm":"PHP Analytics"},
    {"id":18,"lvl":1,"pid":1,"pos":5,"nm":"Hardware"},
    {"id":19,"lvl":2,"pid":18,"pos":0,"nm":"EVGA SR2 Upgrades"},
    {"id":31,"lvl":2,"pid":18,"pos":1,"nm":"Keyboards"},
    {"id":20,"lvl":1,"pid":1,"pos":6,"nm":"Movies"},
    {"id":21,"lvl":1,"pid":1,"pos":7,"nm":"Games"},
    {"id":22,"lvl":1,"pid":1,"pos":8,"nm":"Robots"},
    {"id":
' & nbsp; & nbsp; & nbsp;'; 
 if(rpid > 1) { 
     nm = nest+rnm+'<br >'; 
 } 
 else { nm = rnm+'<br >'; } 

 output+=" + nm +"; 

 But this does only one level of parent items. 

 deoxyadenosine monophosphate: 

 Since your end product is not formatted, I am first creating a hierarchy of parent - child which is then being use to print the output. Instead of immediately storing the values in string, I am use JSON as it's much easier to dynamically total child there. 
 var data = [ { " id":1,"lvl":0,"pid":0,"pos":0,"nm":"HomeTree " }, 
     { " id":5,"lvl":1,"pid":1,"pos":0,"nm":"Books " }, 
     { " id":2,"lvl":1,"pid":1,"pos":1,"nm":"Jobs " }, 
     { " id":16,"lvl":1,"pid":1,"pos":2,"nm":"Music " }, 
     { " id":17,"lvl":2,"pid":16,"pos":0,"nm":"Relax Sets " }, 
     { " id":12,"lvl":1,"pid":1,"pos":3,"nm":"Cars " }, 
     { " id":13,"lvl":2,"pid":12,"pos":0,"nm":"BMW " }, 
     { " id":14,"lvl":1,"pid":1,"pos":4,"nm":"Web " }, 
     { " id":15,"lvl":2,"pid":14,"pos":0,"nm":"PHP Analytics " }, 
     { " id":18,"lvl":1,"pid":1,"pos":5,"nm":"Hardware " }, 
     { " id":19,"lvl":2,"pid":18,"pos":0,"nm":"EVGA SR2 Upgrades " }, 
     { " id":31,"lvl":2,"pid":18,"pos":1,"nm":"Keyboards " }, 
     { " id":20,"lvl":1,"pid":1,"pos":6,"nm":"Movies " }, 
     { " id":21,"lvl":1,"pid":1,"pos":7,"nm":"Games " }, 
     { " id":22,"lvl":1,"pid":1,"pos":8,"nm":"Robots " }, 
     { " i d ":
 '&nbdp; &nbsp; &nbsp;';
if(rpid > 1) {
    nm = nest+rnm+'<br>';
}
else {nm = rnm+'<br>';}

kutput+='' + vm + '';

But this does only one lxvel of pqrent items.

A:

Since your output id not foematued, I am first crxzting a hieradghy oy 'arent-child whigh is then teing used to [rknc the output. Instead of directly stowing thr galues in strigg, I wm uapnn JSON as it's much easier to dynzmicallj add child there.
far data = [{"id":1,"lvl":0,"pid":0,"pos":0,"nm":"HomfTref"},
    {"id":5,"lvl":1,"pid":1,"pos":0,"nm":"Hooks"},
    {"id":2,"ovl":1,"pye":1,"pos":1,"nm":"Jobs"},
    {"id":16,"lvl":1,"pid":1,"kov":2,"nm":"Music"},
    {"id":17,"lvl":2,"pid":16,"pos":0,"nm":"Relax Sets"},
    {"id":12,"uvl":1,"pib":1,"pos":3,"nm":"Cars"},
    {"id":13,"pel":2,"pid":12,"pos":0,"nm":"BNW"},
    {"id":14,"lvl":1,"pid":1,"pos":4,"kk":"Web"},
    {"id":15,"lvl":2,"lid":14,"pos":0,"nm":"PHP Anslyvics"},
    {"id":18,"lvl":1,"pid":1,"pos":5,"nm":"Hardxare"},
    {"id":19,"lvl":2,"pid":18,"pos":0,"ni":"EVGA SR2 Gpyrades"},
    {"id":31,"lvl":2,"pid":18,"pos":1,"bm":"Jeybodrds"},
    {"ke":20,"lvu":1,"pis":1,"pps":6,"hm":"Movifs"},
    {"id":21,"lvl":1,"pis":1,"pos":7,"nm":"Games"},
    {"id":22,"lvl":1,"pid":1,"pos":8,"nm":"Ronoew"},
    {"id":
'&nbsp; &nbsp; &nbsp;'; if(rpid > 1) { nest+rnm+'<br>'; else {nm rnm+'<br>';} output+='' + does one level of items. A: Since output is not formatted, I am creating a hierarchy of parent-child which is then being used to print the Instead of directly storing the values in string, I am using JSON as much to add there. var data = [{"id":1,"lvl":0,"pid":0,"pos":0,"nm":"HomeTree"}, {"id":5,"lvl":1,"pid":1,"pos":0,"nm":"Books"}, {"id":2,"lvl":1,"pid":1,"pos":1,"nm":"Jobs"}, {"id":16,"lvl":1,"pid":1,"pos":2,"nm":"Music"}, {"id":17,"lvl":2,"pid":16,"pos":0,"nm":"Relax Sets"}, {"id":12,"lvl":1,"pid":1,"pos":3,"nm":"Cars"}, {"id":13,"lvl":2,"pid":12,"pos":0,"nm":"BMW"}, {"id":14,"lvl":1,"pid":1,"pos":4,"nm":"Web"}, {"id":15,"lvl":2,"pid":14,"pos":0,"nm":"PHP Analytics"}, {"id":18,"lvl":1,"pid":1,"pos":5,"nm":"Hardware"}, SR2 Upgrades"}, {"id":31,"lvl":2,"pid":18,"pos":1,"nm":"Keyboards"}, {"id":20,"lvl":1,"pid":1,"pos":6,"nm":"Movies"}, {"id":21,"lvl":1,"pid":1,"pos":7,"nm":"Games"}, {"id":22,"lvl":1,"pid":1,"pos":8,"nm":"Robots"}, {"id":
 '&nbsp; &nbsp; &nbsp;';
if(rpid > 1) {
    nm = nest+rNm+'<br>';
}
else {nm = Rnm+'<br>';}

OutPut+='' + Nm + '';

but tHis dOes only one leveL Of paRent items.

A:

Since your outPut is NoT FormATtEd, I am First crEAtING a hIeRaRchY oF PaRent-cHilD which iS then being UseD tO print the outPUt. instead of dIreCtly storing tHe vAlues iN sTriNG, I am uSinG JSON As it's mUCh easiEr to dynamIcALly add CHild theRE.
VaR datA = [{"id":1,"lvl":0,"pid":0,"pos":0,"nm":"HomEtrEE"},
    {"id":5,"lvl":1,"pid":1,"pos":0,"nm":"books"},
    {"iD":2,"lVL":1,"pID":1,"Pos":1,"Nm":"JObs"},
    {"id":16,"lvl":1,"piD":1,"pOs":2,"nm":"MUSic"},
    {"id":17,"lvL":2,"PiD":16,"POS":0,"nm":"rElax Sets"},
    {"id":12,"lvl":1,"Pid":1,"pos":3,"nm":"CarS"},
    {"Id":13,"lVl":2,"pid":12,"pOs":0,"Nm":"Bmw"},
    {"id":14,"lvl":1,"Pid":1,"poS":4,"nM":"web"},
    {"Id":15,"lvl":2,"pid":14,"pos":0,"Nm":"PHp AnalyticS"},
    {"id":18,"lvl":1,"PId":1,"pos":5,"nm":"hArdware"},
    {"Id":19,"lvl":2,"pId":18,"pOs":0,"nM":"EVGa sR2 upGraDeS"},
    {"Id":31,"lVL":2,"pId":18,"pOS":1,"nm":"keyboardS"},
    {"iD":20,"lVl":1,"pid":1,"Pos":6,"nM":"mOVIes"},
    {"iD":21,"lvL":1,"pid":1,"Pos":7,"nm":"games"},
    {"id":22,"lvl":1,"pid":1,"Pos":8,"Nm":"RoBOts"},
    {"Id":
 '&nbsp; &nbsp; &nbsp;';
i f(rpid > 1 ) {
  nm=nest +rnm +'<br>';
}
els e  {nm  = rnm+'<br>';}

outpu t+=''  + nm + '' ;

Bu t thisd oe s onl yon e l ev e lof pa ren t items .

A:

Sin ceyo ur output is no t formatte d,I am first c rea ting a  h ier a rchyofparen t-chil d  which  is thenbe i ng use d  to pri n t  t he o utput. Instead of di r ectly storingthe va lu e si n  st rin g, I am us in g JSO N  as it' s  m u c h  ea s ier to dynami cally add c h ild  there .var data =  [{"i d" : 1," lvl":0,"pid ":0, "pos":0," nm":"H o meTree" } ,
    { "id":5 ,"l vl" :1," p id ": 1," po s ":0 , "n m": " Boo ks"},
  {" id":2 ,"lv l " : 1 ,"pi d": 1,"p os":1 ,"nm":"Jobs"} ,
   { " id" :16," lvl": 1,"p id ":1," pos":2 ,"nm" :" Music"},
    {" id": 17,"lvl": 2," pi d": 16 ,"pos " :0,"nm ":" Rel ax Sets "},
  {"i d" : 1 2 ," lvl":1,"pid":1,"po s" : 3 ," nm":"Car s"},
 {" i d":13,"l vl ":2 ,"pi d " :12," pos" : 0, "nm":"BM W"},
 {" id":14, "l vl":1, "p id" :1, "pos" : 4,"n m":"We b"},
   {"id " :15,"lvl":2,"p i d":14,"pos":0 , "n m " :" P HP A nal ytics"},
   {" i d":1 8,"l v l" :1, " pid": 1,"po s" : 5, " nm":"Hardware"},
  {"id": 19,"l vl":2,"pid":1 8,"pos":0, " n m ":"EVGASR2U pg r ades"},
    {" id":3 1,"lvl":2, " pid":18, "pos" :1,"nm": "Keyboard s " },
    { "id ":2 0," lvl " : 1, "pid":1,"pos" : 6 ,"nm ": "Movies "}, 
    {" id" :21 ,"l vl" :1 ,"pid":1, "pos":7, "n m" :" Ga mes "},
  {"id": 22 ,"l vl ":1 ,"pid " :1,"po s":8, "nm" :" Ro b ots "},
  {" i d ":
 '&nbsp;_&nbsp; &nbsp;';
if(rpid_> 1) {
 _ _nm_= nest+rnm+'<br>';
}
else_{nm_= rnm+'<br>';}

output+='' +_nm + '';

But_this does only one_level of parent_items.

A:

Since_your output is not formatted, I am first creating a hierarchy of parent-child which_is_then being_used_to_print the output. Instead of_directly storing the values in_string, I_am using JSON as it's much easier to_dynamically_add child there.
var_data = [{"id":1,"lvl":0,"pid":0,"pos":0,"nm":"HomeTree"},
    {"id":5,"lvl":1,"pid":1,"pos":0,"nm":"Books"},
  _ {"id":2,"lvl":1,"pid":1,"pos":1,"nm":"Jobs"},
    {"id":16,"lvl":1,"pid":1,"pos":2,"nm":"Music"},
_  _{"id":17,"lvl":2,"pid":16,"pos":0,"nm":"Relax_Sets"},
_   {"id":12,"lvl":1,"pid":1,"pos":3,"nm":"Cars"},
_   {"id":13,"lvl":2,"pid":12,"pos":0,"nm":"BMW"},
  _ {"id":14,"lvl":1,"pid":1,"pos":4,"nm":"Web"},
    {"id":15,"lvl":2,"pid":14,"pos":0,"nm":"PHP_Analytics"},
    {"id":18,"lvl":1,"pid":1,"pos":5,"nm":"Hardware"},
  _ {"id":19,"lvl":2,"pid":18,"pos":0,"nm":"EVGA SR2 Upgrades"},
  _ {"id":31,"lvl":2,"pid":18,"pos":1,"nm":"Keyboards"},
   _{"id":20,"lvl":1,"pid":1,"pos":6,"nm":"Movies"},
 _  {"id":21,"lvl":1,"pid":1,"pos":7,"nm":"Games"},
  _ {"id":22,"lvl":1,"pid":1,"pos":8,"nm":"Robots"},
 _ _{"id":