从JSON目录树中的文件完整路径计算目录

时间:2019-10-23 16:54:48

标签: javascript recursion tree

{
  "path": null,//should be calculated back as: "photos"
  "size": 600,
  "type": "directory",
  "children": [
    {
      "path": null,//it should be calculated as: "photos/summer"
      "size": 400,
      "type": "directory",
      "children": [
        {
          "path": null,//should be calculated as :"photos/summer/june"
          "size": 400,
          "type": "directory",
          "children": [
            {
              "path": "photos/summer/june/windsurf.jpg",
              "name": "windsurf.jpg",
              "size": 400,
              "type": "file",
              "extension": ".jpg"
            }
          ]
        }
      ]
    },
    {
      "path": null,//should be calculated as: "photos/winter"
      "size": 200,
      "type": "directory",
      "children": [
        {
          "path": null,// should be calculated as: "photos/winter/january"
          "size": 200,
          "type": "directory",
          "children": [
            {
              "path": "photos/winter/january/ski.png",
              "name": "ski.png",
              "size": 100,
              "type": "file",
              "extension": ".png"
            },
            {
              "path": "photos/winter/january/snowboard.jpg",
              "name": "snowboard.jpg",
              "size": 100,
              "type": "file",
              "extension": ".jpg"
            }
          ]
        }
      ]
    }
  ]
}

有一个代表目录结构的json。 json中的所有“文件”属性都有分配给属性“路径”的绝对路径。 但是每个子目录/目录都缺少“路径”值。每个具有path = null的子目录都需要根据定义了绝对路径的最深子级(type =“ file”)分配路径。(预期输出注释为 //应该计算为:

我尝试了迭代方法,但是问题是我必须从json深度遍历到顶部(即,最深的孩子朝着parent)。有人可以推荐更清洁的方法吗?

2 个答案:

答案 0 :(得分:1)

您可以通过获取path属性来设置name并迭代children(如果存在)并移交最后一条路径。

function setPath(object, path = '') {
    (object.children || []).forEach(o => {
        var temp = setPath(o);
        if (temp) object.path = temp.slice(0, temp.lastIndexOf('/'));
    });
    return object.path;
}

var data = { path: null, size: 600, type: "directory", children: [{ path: null, size: 400, type: "directory", children: [{ path: null, size: 400, type: "directory", children: [{ path: "photos/summer/june/windsurf.jpg", name: "windsurf.jpg", size: 400, type: "file", extension: ".jpg" }] }] }, { path: null, size: 200, type: "directory", children: [{ path: null, size: 200, type: "directory", children: [{ path: "photos/winter/january/ski.png", name: "ski.png", size: 100, type: "file", extension: ".png" }, { path: "photos/winter/january/snowboard.jpg", name: "snowboard.jpg", size: 100, type: "file", extension: ".jpg" }] }] }] };

setPath(data);

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

答案 1 :(得分:0)

您可以尝试一下,

function setPath(obj,path){
    let currentPath=path+'/'+obj.name;
    obj.path=currentPath;
    if(obj.children && obj.children.length){
        obj.children.forEach(item=>this.setPath(item,currentPath))
    }
}

以对象和路径名称为“ /”的方式调用此函数 例如setPath(obj,'/')