如何从具有绝对路径的对象数组构造类似于文件系统的JSON

时间:2019-01-24 16:04:41

标签: javascript json

我正在尝试为fileSystem创建JSON文件。我在this post中发现了一些有趣的东西,但是我需要得到这样的结果:

{path: 'root',
  size: 24,
  isEmpty: false,
  isDir: true,
  lastModified: 156727373,
  children: [
        {path: 'root/folder1',
         size: 24,
         isEmpty: false,
         isDir: true,
         lastModified: 156727373,
         children: [{
               size:10,
               name:'file1',
               isDir:true,
               isEmpty:true,
               lastModified:10123123123
        }, {
               size:10,
               name:"file2",
               isDir:true,
               isEmpty:true,
               lastModified:10123123123
        }, {
               size:10,
               name:'file3',
               isDir:true,
               isEmpty:true,
               lastModified:10123123123}]}

来自具有键“ path”和绝对路径值的数据数组。像这样:

data:[{
    "path":"/root/folder1",
    "children":[
         {
            "size":10,
            "name":"file1",
            "isDir":true,
            "isEmpty":true,
            "lastModified":10123123123
         },
         {
            "size":10,
            "name":"file2",
            "isDir":true,
            "isEmpty":false,
            "lastModified":10123123123
         },
         {
            "size":10,
            "name":"file3",
            "isDir":false,
            "isEmpty":true,
            "lastModified":10123123123
         }
      ]}, {
    "path":"/root",
    "children":[
         {
            "size":10,
            "name":"folder1",
            "isDir":true,
            "isEmpty":false,
            "lastModified":10123123123
         }
      ]}
]

详细信息: 我有一个数据库表,其中每一行代表一个目录,其中包含:绝对路径和带有其信息的子级。单击子目录将触发异步请求,询问该目录的子目录数据,数据库将创建该目录数据的一行。绝对路径将用于对整个文件系统树中的JSON中的各个文件夹进行排序。

我尝试过:

var output = {};
var current;

for(var a=0; a<data.length; a++) {
  var s = data[a].path.split('/');
  current = output;
  for(var i=0; i<s.length; i++) {
    if(s[i] != '') {
      if(current[s[i]] == null) current[s[i]] = {};
      current = current[s[i]];
    }
  }
}

但是我不需要设置一个空对象(如果它不存在),而我唯一能得到的结果就是一个圆形结构,因此任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以采用给定的路径和每个子代的名称来找到该对象。然后更新对象。

var data = [{ path: "/root/folder1", children: [{ size: 10, name: "file1", isDir: true, isEmpty: true, lastModified: 10123123123 }, { size: 10, name: "file2", isDir: true, isEmpty: false, lastModified: 10123123123 }, { size: 10, name: "file3", isDir: false, isEmpty: true, lastModified: 10123123123 }] }, { path: "/root", children: [{ size: 10, name: "folder1", isDir: true, isEmpty: false, lastModified: 10123123123 }] }],
    result = data.reduce((r, { path, children }) => {
        var temp = path.slice(1).split('/');
        children.forEach((o) => {
            Object.assign(
                temp
                    .concat(o.name)
                    .map((_, i, a) => a.slice(0, i + 1).join('/'))
                    .reduce((q, path, i) => {
                        var temp = (q.children = q.children || []).find(p => p.path === path);
                        if (!temp) q.children.push(temp = { path });
                        return temp;
                    }, r),
                o
            );
        });
        return r;
    }, {}).children[0];

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }