-1

I have the following list of files:

const paths = [
  "/apps/text-open/app.js",
  "/apps/html-open/app.js",
  "/apps/editor/app.js",
  "/apps/editor/index.html",
  "/Images/image.png",
  "/Documents/README.txt",
  "/Documents/example.txt",
]

This is stored in localStorage and I have a function that takes a tree (format given later) and renders it to html. The tree for the above list would be this:

const tree = {
  label: "/",
  children: [{
      label: "apps",
      path: "/apps",
      children: [{
          label: "text-open",
          path: "/apps/text-open",
          children: [{
            label: "app.js",
            path: "/apps/text-open/app.js"
          }, ]
        },
        {
          label: "html-open",
          path: "/apps/html-open",
          children: [{
            label: "app.js",
            path: "/apps/html-open/app.js"
          }, ]
        },
        {
          label: "editor",
          path: "/apps/editor",
          children: [{
              label: "app.js",
              path: "/apps/editor/app.js"
            },
            {
              label: "index.html",
              path: "/apps/editor/index.html"
            },
          ]
        },
      ]
    },
    {
      label: "Documents",
      path: "/Documents",
      children: [{
          label: "README.txt",
          path: "/Documents/README.txt"
        },
        {
          label: "example.txt",
          path: "/Documents/example.txt"
        },
      ]
    },
    {
      label: "Images",
      path: "/Images",
      children: [{
        label: "image.png",
        path: "/Images/image.png"
      }, ]
    },
  ],
}

How could I convert the list of paths to this tree programatically?

I need this to generate directories dynamically. They are not supplied in the list and have to be inferred by the fact that files are in them.

0 Answers0