Flat directory path to heirarchy

Viewed 798

I have a flat list of directory paths. I want to convert that to a hierarchy.

Input format:

var paths = [
  "A/B",
  "A/B/C",
  "A/B/C/D",
  "A/B/C/E",
    "F" 
];

Output format

[
  {
    "name": "A",
    "children": [
      {
        "name": "B",
        "children": [
          {
            "name": "C",
            "children": [
              {
                "name": "D",
                "children": []
              },
              {
                "name": "E",
                "children": []
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "name": "F",
    "children": []
  }
]

In my input ,I cannot have any parent parameter. My in progress fiddle is here - https://jsfiddle.net/ashwyn/Laamg14z/2/

Thanks.

1 Answers
Related