title: Efficient function for creating JSON data from File Directory Structure?
body: <p>Like the title says, I have a directory structure, I want to convert it into a JSON format compatible for <a href="http://www.jstree.com/docs/json/" rel="nofollow">jsTree usage</a>.
  So the output for a given list</p>
  
  <p><strong>INPUT:</strong></p>
  
  <pre><code>./Simple Root Node
  ./Root Node 2
  ./Root Node 2/Child 1
  ./Root Node 2/Child 2
  </code></pre>
  
  <p><strong>OUTPUT:</strong></p>
  
  <pre><code>treeJSON = [
         { "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
         { "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
         { "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
         { "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
      ]
  </code></pre>
  
  <p><strong>My Method:</strong></p>
  
  <p>Currently, I'm taking each line from the input. Say <code>./Root Node 2/Child 1</code>, then I pattern match the first folder, creating an array like <code>{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" }</code>. Then go recursively for the next removing the first folder.Hence, creating the net array as <code>{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" }</code>. </p>
  
  <p>I do this for each line in the input and then use my unique array function as in <a href="http://jsfiddle.net/bsw5s60j/8/" rel="nofollow">http://jsfiddle.net/bsw5s60j/8/</a> to strip all the duplicate arrays which were created. For instance, <code>{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" }</code> would be created twice. Once while going through the 3rd line and then the 4th line.</p>
  
  <p>Clearly, this code is HIGHLY inefficient.If I have around 1.3K directories, then assume each has 4 sub directories, we have 5.2K arrays which have to be checked for duplicates.</p>
  
  <p>This is creating a hge problem. Is there any other efficient way I can twaek this code?</p>
  
  <p><strong>Fiddle: (Works with Chrome Only because of the file webkit attribute)</strong>  <a href="http://jsfiddle.net/bsw5s60j/8/" rel="nofollow">http://jsfiddle.net/bsw5s60j/8/</a></p>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code> var input = document.getElementById('files');
   var narr = [];
   var fileICON = "file.png";
  
   //when browse button is pressed
   input.onchange = function (e) {
       var dummyObj = [];
       var files = e.target.files; // FileList
       for (var i = 0, f; f = files[i]; ++i) {
           var fname = './' + files[i].webkitRelativePath;
           narr = $.merge(dummyObj, (cat(fname)));
       }
  
       treeJSON = narr.getUnique(); // getting the JSON tree after processing input
       console.log(JSON.stringify(treeJSON));
  
       //creating the tree using jstree
  
       $('#tree')
           .jstree({
           'core': {
               'check_callback': true,
                   'data': function (node, cb) {
                   cb.call(this, treeJSON);
               }
           }
       });
       var tree = $('#tree').jstree(true);
       tree.refresh();
   };
  
   //get unqiue array function
   Array.prototype.getUnique = function () {
       var o = {}, a = [];
       for (var i = 0, l = this.length; i &lt; l; ++i) {
           if (o.hasOwnProperty(JSON.stringify(this[i]))) {
               continue;
           }
           a.push(this[i]);
           o[JSON.stringify(this[i])] = 1;
       }
       return a;
   };
  
   // categorizing function which converts each ./Files/Root/File.jpg to a JSON
  
   var objArr = [];
   var folderArr = [];
  
   function cat(a) {
       if (!a.match(/\/(.+?)\//)) {
           var dummyObj = {};
           var fname = a.match(/\/(.*)/)[1];
           dummyObj.id = fname;
           dummyObj.text = fname;
           if (folderArr === undefined || folderArr.length == 0) {
               dummyObj.parent = '#';
           } else {
               dummyObj.parent = folderArr[(folderArr.length) - 1];
               dummyObj.icon = fileICON; // add extention and icon support
           }
           objArr.push(dummyObj);
           return objArr;
       } else {
           if (a.charAt(0) == '.') {
               var dummyObj = {};
               var dir1 = a.match(/^.*?\/(.*?)\//)[1];
               dummyObj.id = dir1;
               dummyObj.text = dir1;
               dummyObj.parent = '#';
               dummyObj.state = {
                   'opened': true,
                       'selected': true
               }; // not working
               folderArr.push(dir1);
               objArr.push(dummyObj);
               var remStr = a.replace(/^[^\/]*\/[^\/]+/, '');
               cat(remStr);
               return objArr;
           } else {
               var dummyObj = {};
               var dir1 = a.match(/^.*?\/(.*?)\//)[1];
               dummyObj.id = dir1;
               dummyObj.text = dir1;
               dummyObj.parent = folderArr[(folderArr.length) - 1];
               folderArr.push(dir1);
               objArr.push(dummyObj);
               var remStr = a.replace(/^[^\/]*\/[^\/]+/, '');
               cat(remStr);
               return objArr;
           }
       }
   }
  </code></pre>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>    &lt;input type="file" id="files" name="files[]" multiple webkitdirectory /&gt;
  &lt;div id="tree"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Any changes or suggestions would be greatly helpful! Thanks</p>