0

I'm working in JavaScript, and have an array of filepaths:

['root/folder1', 'root/folder1/folderA', 'root/folder1/folderA/folderi', 'root/folder1/folderB', 'root/folder2', ...] 

and so on. The filepaths are in order, so 'folder1' will always be listed before 'folder1/folderA', and so on.

I need to convert this to a tree structure, which is leaving me stumped. I'm aiming for something like this (the next step will have files in each folder as well, so I need to prep for that):

allFolders = { root: { 'files': [], 'folders': { folder1: { 'files': [], 'folders': { folderA: {...}, folderB: {...} } }, folder2: {} } } } 

although I'm open to any structure that works. I need to be able to iterate over the final result to display a file tree like this:

root - folder1 -- folderA --- folderi -- folderB - folder2 

I don't really have any code to show because I haven't got as far as producing anything that's even remotely useful.

3
  • 2
    Please show us your attempts, even if they don't work. Commented Jul 15, 2024 at 13:52
  • creating a loop inside the loop might work. Is this for collecting data or creating a structure like that ? Commented Jul 15, 2024 at 13:58
  • Thanks - have been given links to related questions, so I'll check those out. Commented Jul 15, 2024 at 14:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.