解析文件路径字符串到json对象

沙吉

我有如下网址列表。如何将其转换为完整的json对象?

我的网址数组。

[
"path1/subpath1/file1.doc","path1/subpath1/file2.doc","path1/subpath2/file1.doc","path1/subpath2/file2.doc","path2/subpath1/file1.doc","path2/subpath1/file2.doc","path2/subpath2/file1.doc","path2/subpath2/file2.doc","path2/subpath2/additionalpath1/file1.doc"
]

我想要像这样的json对象:

{
   "path1":{
      "subpath1":["file1.doc","file2.doc"],
      "subpath2":["file1.doc","file2.doc"]
   },
   "path2":{
      "subpath1":["file1.doc","file2.doc"],
      "subpath2":["file1.doc","file2.doc"],
      "additionalpath1":{
          "additionalpath1":["file1.doc"]
      }
   }
}

这该怎么做?

我尝试了以下代码段。但是缺少一些文件夹对象。

如果您尝试使用此代码,则会发现该代码test1以及其他objects缺少的代码:

<html>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<body>


<p id="demo"></p>

<script>
let paths = [ "admin/640954.jpg", "admin/test1/3m-nd.jpg", 
"admin/test1/Acct.png", "admin/test1/additional/111.gif", "admin/test1/additional/Aard.jpg", 
"dp/151292.jpg", "dp/151269.jpg", "dp/1515991.jpg" ];


function getMap(urls){
    var map = {};
    urls.forEach(function(url){
        var parts = url.split("/");
        makePath(map, parts);
    })
    return map;
}

function makePath(map,parts){
    var currentPath = map;
    for(var i = 0 ; i < parts.length - 1 ; i++ ){
            if(i == parts.length -2 ){
                currentPath[parts[i]]  = currentPath[parts[i]] || [];
                currentPath[parts[i]].push(parts[++i]);
            }else{
                currentPath[parts[i]] =  currentPath[parts[i]] || {};
                currentPath = currentPath[parts[i]];
            }
    }

}

document.getElementById("demo").innerHTML=JSON.stringify(getMap(paths));
</script>

</body>
</html>
初级

您可以使用.split("/")并遍历结果,在嵌套对象中创建属性:

let paths = [
  "path1/subpath1/file111.doc",
  "path1/subpath1/file112.doc",
  "path1/subpath2/file121.doc",
  "path1/subpath2/file122.doc",
  "path2/subpath1/file211.doc",
  "path2/subpath1/file212.doc",
  "path2/subpath2/file221.doc",
  "path2/subpath2/file222.doc",
  "path2/additionalpath3/additionalpath1/file2311.doc"
];

let treePath = {};
paths.forEach(path => {
  let levels = path.split("/");
  let file = levels.pop();

  let prevLevel = treePath;
  let prevProp = levels.shift();

  levels.forEach(prop => {
    prevLevel[prevProp] = prevLevel[prevProp] || {};
    prevLevel = prevLevel[prevProp];
    prevProp = prop;
  });

  prevLevel[prevProp] = (prevLevel[prevProp] || []).concat([file]);
});

console.log(treePath);

要么:

let paths = [
  "path1/subpath1/file111.doc",
  "path1/subpath1/file112.doc",
  "path1/subpath2/file121.doc",
  "path1/subpath2/file122.doc",
  "path2/subpath1/file211.doc",
  "path2/subpath1/file212.doc",
  "path2/subpath2/file221.doc",
  "path2/subpath2/file222.doc",
  "path2/additionalpath3/additionalpath1/file2311.doc"
];

let treePath = {};
paths.forEach(path => {
  let levels = path.split("/");
  let file = levels.pop();

  levels.reduce((prev, lvl, i) => {
    return prev[lvl] = (levels.length - i - 1) ? prev[lvl] || {} : (prev[lvl] || []).concat([file]);
  }, treePath);
});

console.log(treePath);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档