0

I have a nested object tree which represents a folder structure. I need to build multiple directories by traversing through the nodes.

diagram of folder structure:

enter image description here

Here's my folder structure's object: https://jsfiddle.net/himel023/8jw3yhs0/

I have tried recursive and non-recursive approach. Not working the way i wanted it to.

Here's my jsfiddle link: https://jsfiddle.net/himel023/sr25xq8L/

some sample code that i have tried:

let crDirList = (tree) => {
  for (let i = 0; i < tree.length; i++) {
    let tempSingleDir = crSingleDir(tree[i]);
    console.log(tempSingleDir);
    let curDir = tree[i];

    while (curDir.hasChild) {

      curDir = curDir.subFolder.folders;
      crDirList(curDir);

    }
  }

}

let dirArr = [];

function crSingleDir (tree) {
  let dirToBuildArr = [];

  let subf = tree.subFolder.folders;
  if (tree.hasChild) {
    for (let i = 0; i < subf.length; i++) {
      dirToBuildArr[i] = `/${tree.name}/${subf[i].name}`;
    }
  } else {
    dirToBuildArr[0] = 0;
  }
  return(dirToBuildArr);
}

let fst = treeObject.folders;
crDirList(fst.folders);

I am getting only one level of subfolders for e.g. "/video/Synopi-Videos" or "/Other videos/Urgent folder".

But what i am trying to do is getting some outputs like "/video/Synopi-Videos/Empty Folders" , then again "/video/Synopi-Videos/Other Videos/Urget folder/" etc.

4
  • I was just doing something similar. Have a look at 31645738. This related post helped me. Commented May 7, 2019 at 6:17
  • 1
    Any particular reason you're using that folder structure? There's a lot of things that could be done to make it a little simpler. For instance, hasChild is redundant. If subFolders exists hasChild should be true. Adds complexity but no value Commented May 7, 2019 at 6:19
  • @RichS I have written the nodejs api for creating a directory if it already does not exist (link: jsfiddle.net/himel023/ypj4otrs ). But the problem i am having is to deriving the directroy structure from object tree, that is needed to pass to that function. Commented May 7, 2019 at 6:32
  • @Tibrogargan You are right. But I thought it would be cleaner for handling codes and stuffs. Rather i am building a single page application so i had to manually assign some ids to the folder which i did not like as well. But i did not find any better workaround. Commented May 7, 2019 at 6:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.