【问题标题】:Calculate Directory from file full path in JSON directory tree从 JSON 目录树中的文件完整路径计算目录
【发布时间】:2019-10-24 01:32:38
【问题描述】:
{
  "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 的子目录都需要根据定义了绝对路径的 deepest child(type="file") 分配路径。(预期输出注释为 //应计算为:)

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

【问题讨论】:

    标签: javascript recursion tree


    【解决方案1】:

    您可以通过获取name 属性来设置path 并迭代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; }

    【讨论】:

    • 感谢您的建议。但问题是:name 属性只是一个标签或显示名称,它可以是任意字符串。更新了 json 格式,其中 name 根本不可用。
    • 谢谢@Nina。感谢您的帮助
    【解决方案2】:

    你可以试试这个,

    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,'/')

    【讨论】:

    • 感谢您的建议。但问题是:name 属性只是一个标签或显示名称,它可以是任意字符串。更新了json格式,其中name根本不可用
    • 那么我们需要根据哪个标签或字段名称来创建路径?如果名称字段不存在。
    • 谢谢。基于“type”=“file”所具有的“path”。
    • 对不起,我没弄明白,那么我将如何获得目录名称,例如“photos/summer/june/windsurf.jpg”在这个照片和夏天是目录对吗?我将如何获得这些名字?
    猜你喜欢
    • 2016-05-16
    • 2010-10-14
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多