The recursion is to not overload the thread so you need to create two functions something like this:
function buildChildren(childrenObject){
let length = childrenObject.length; // Brought outside the loop due to improved execution times.
for (let i = 0; i < length; i++) {
let child = childrenObject[i];
// Do stuff with the child here, such as building it and attaching it do the DOM.
fetchChildren(child);
}
}
function fetchChildren(child){
if (child.children.length > 0) {
buildChildren(child.children);
}
}
This is essentially what you want to do. This does not throw recursion errors but will take a while if the array you have is of significant size.