当前位置:  开发笔记 > 编程语言 > 正文

Javascript:将字符串路径转换为格式化数组

如何解决《Javascript:将字符串路径转换为格式化数组》经验,为你挑选了1个好方法。

所以我试图使用github api并将其转换为用于dynatree.

我期待使用javascript.但如果在Ruby,ClojureScript等中更容易完成,那么也可以.

所以Github api返回的内容如下:

 {
 "sha": "a1f28431d5ee1a00361ff7e1e2dc91172d5bbabd",
 "url": "https://api.github.com/repos/Jonovono/c/git/trees/a1f28431d5ee1a00361ff7e1e2dc91172d5bbabd",
"tree": [
{
  "mode": "100644",
  "type": "blob",
  "sha": "b81f704ee24f08f5f3e00675a9912da79274c3bc",
  "path": ".gitignore",
  "size": 114,
  "url":  "https://api.github.com/repos/Jonovono/c/git/blobs/b81f704ee24f08f5f3e00675a9912da79274c3bc"
  },
   {
  "mode": "100644",
  "type": "blob",
  "sha": "c6231c2f161f50bededaaac624a7a96db6179e9a",
  "path": "Makefile.am",
  "size": 40,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/c6231c2f161f50bededaaac624a7a96db6179e9a"
   },
  {
  "mode": "100644",
  "type": "blob",
  "sha": "ca41e5b8fb710b4ae7a2cdb35cdbafb143382dc2",
  "path": "README.md",
  "size": 2194,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/ca41e5b8fb710b4ae7a2cdb35cdbafb143382dc2"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "dd3b02c5fcf3312ef7a83f4b6370a8b546739bc1",
  "path": "config.h.in",
  "size": 625,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/dd3b02c5fcf3312ef7a83f4b6370a8b546739bc1"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "e242df0dc8be1fec7b2da2091ab8161aaee1b7a2",
  "path": "configure.ac",
  "size": 156,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/e242df0dc8be1fec7b2da2091ab8161aaee1b7a2"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "df8eea7e4ce8862105fcd7929b20bdb45488048b",
  "path": "depcomp",
  "size": 18615,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/df8eea7e4ce8862105fcd7929b20bdb45488048b"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "6781b987bdbcbc23efe6bbe1654a1e3637b9af07",
  "path": "install-sh",
  "size": 13663,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/6781b987bdbcbc23efe6bbe1654a1e3637b9af07"
},
{
  "mode": "100755",
  "type": "blob",
  "sha": "28055d2ae6f2a2c584afcd769d7881e11f62ecd9",
  "path": "missing",
  "size": 11419,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/28055d2ae6f2a2c584afcd769d7881e11f62ecd9"
},
{
  "mode": "040000",
  "type": "tree",
  "sha": "57bb4a62d2de582c74b2e6de71e3024db7905cac",
  "path": "src",
  "url": "https://api.github.com/repos/Jonovono/c/git/trees/57bb4a62d2de582c74b2e6de71e3024db7905cac"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "daaed524883bd510502834d472e9be6cff4e3a69",
  "path": "src/Makefile.am",
  "size": 59,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/daaed524883bd510502834d472e9be6cff4e3a69"
},
{
  "mode": "100644",
  "type": "blob",
  "sha": "cf3f8f549fe870315eb4417c9f84dec587c8286b",
  "path": "src/c.c",
  "size": 9480,
  "url": "https://api.github.com/repos/Jonovono/c/git/blobs/cf3f8f549fe870315eb4417c9f84dec587c8286b"
}
 ]
}

所以基本上我想得到数组中的所有路径,如"Makefile.am","src"(识别是一个文件夹),"src/cc".这也适用于递归文件夹.

dynatree的格式如下:

  var obj = [
    { title: 'Lazy node 1', isLazy: true },
    { title: 'Lazy node 2', isLazy: true },
    { title: 'Folder node 3', isFolder: true,
      children: [
        { title: 'node 3.1' },
        { title: 'node 3.2',
          children: [
            { title: 'node 3.2.1' },
            { title: 'node 3.2.2',
              children: [
                { title: 'node 3.2.2.1' }
              ]
            }
          ]
        }
      ]
    }
  ];

要么

  var rootNode = $("#tree").dynatree("getRoot");
  var childNode = rootNode.addChild({
    title: "Programatically addded nodes",
    tooltip: "This folder and all child nodes were added programmatically.",
    isFolder: true
  });
  childNode.addChild({
    title: "Document using a custom icon",
    icon: "customdoc1.gif"
  });

我不确定哪一个更容易到达.我想我可以使用某种类型的哈希表,但我不知道如何让它以递归方式工作,例如:"src/a/b/c/index.text"



1> Benjamin Gru..:

这是一个简单的解决方案,它采用类似字符串"/src/hello/world"并将其路径添加到对象

files = {};
function parseFolder(path){
    var cur = files;
    path.split("/").slice(1).forEach(function(elem){
        cur[elem] = cur[elem] || {}; 
        cur = cur[elem];
    });
}

用法就像是

parseFolder("/src/stuff/hello");
parseFolder("/src/stuff/world");
parseFolder("/bin/stuff/world");

这是一个工作的jsfiddle

这不是Dynatree识别的格式,但是您可以理解,而不是添加一个名为路径的属性,您应该将对象推送到数组.

推荐阅读
喜生-Da
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有