title: Javascript multidimentional array to single object
body: <p>I'm having a hard time writing an algorithm. The context is the following : I'm having an array of paths that I want to put in a single object, like rebuilding a tree of files.</p>
  
  <p>Here's a simple sample : </p>
  
  <pre><code>var paths = [  
    '/var/log/log.txt',  
    '/var/log/test.txt',  
    '/home/toto/someFile.txt',
    '/wtf.txt'  
  ];
  </code></pre>
  
  <p>And I'd like this array of paths to be an object like : </p>
  
  <pre><code>var tree = {  
    var: {
      log: ['log.txt', 'test.txt']
    },
    home: {
      toto: ['someFile.txt']
    },
    wtf.txt: null // I don't know how to handle this case
  };
  </code></pre>
  
  <p>Any hint on how to do such a thing ?</p>
  
  <p>Actually, I've something like this but it ends up with a single depth level and not handling file on root level : </p>
  
  <pre><code>function rebuildTree(paths, tree) {
    paths.forEach(function (path) {
        var splittedPath;
        if (path.indexOf("/") &gt; -1) {
            splittedPath = path.split('/');
        } else {
            splittedPath = [path];
        }
  
        splittedPath.some(function(item, index) {
            if (!tree.hasOwnProperty(item) &amp;&amp; index &gt; 0) {
                tree[item] = {};
            }
  
            if ((parseInt(index) + 1) &lt;= (splittedPath.length - 1)) {
                var nextIndex = parseInt(index + 1);
                var nextPath = splittedPath.splice(0, index);
                tree[item] = rebuildTree(nextPath, tree[item]);
            }
        });
    });
  } 
  </code></pre>