I want to create MLM type tree structure. I have array ARR with all its childs, and again ARR array contain all sub arrays present in it.
Example:
ARR['MainArr'] = aray('child1'=>'child1 val', 'child2'=>'child2 val');
ARR['child1'] = array('subchild1'=>'subchild1 val');
ARR['child2'] = array('...'=>'...');
ARR['subchild1'] = array('...'=>'...');
and so on...
I want to loop all these arrays recursively without using any function(if possible) to get and print all its sub childs till printing all childs.
In the following example parsedAllSpurs
array contains all the sub childs just like ARR
array here.
tmpHN = spurs.hostname;
while(typeof(parsedAllSpurs[tmpHN]) !== 'undefined'){
if(typeof(parsedAllSpurs[tmpHN]) !== 'undefined'){
$.each(parsedAllSpurs[tmpHN], function(k, allSpurs){
tmpHN1 = allSpurs.hostname;
log(tmpHN1);
log(allSpurs);
while(typeof(parsedAllSpurs[tmpHN1]) !== 'undefined'){
if(typeof(parsedAllSpurs[tmpHN1]) !== 'undefined'){
$.each(parsedAllSpurs[tmpHN1], function(j, allSpurs1){
tmpHN2 = allSpurs1.hostname;
//log(tmpHN2);
log(allSpurs1);
while(typeof(parsedAllSpurs[tmpHN2]) !== 'undefined'){
if(typeof(parsedAllSpurs[tmpHN2]) !== 'undefined'){
$.each(parsedAllSpurs[tmpHN2], function(l, allSpurs2){
tmpHN2 = allSpurs2.hostname;
log(allSpurs2);
})
}
}
tmpHN1 = allSpurs1.hostname;
});
}
}
tmpHN = allSpurs.hostname;
});
}
Use recursion instead on nested loops https://en.wikipedia.org/wiki/Recursion. Create function that do it for one level, and call this function each time you found nested structure.
function processStructure(data){
processData(data);
if(hasChildren(data)){
processStructure(data);
}
}
processStructure(initialData);
Hope this helps.