I’m new to Javascript and trying to construct a tree view from a backend response which is a list of AWS S3 objects, my problem is the response needs to be constructed in parent>childeren>children form, so in the frontend can be easily displayed, I want to display a tree like below (mocked tree structure): stackblitz.com/edit/angular-girq88 and below is the service response which I want to construct tree view from: jsoneditoronline.org My goal is to build a recursive function that loops in the response and constructs a tree (deep level) that can be like below:
folder1/ project1/ samplev1.psd samplev2.psd folder2/ project2/ sampleV2.psd
TheSheriff
Co-Founder, Founder, Entrepreneur & Problem Solver
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.