[英]Calculate Directory from file full path in JSON directory 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"
}
]
}
]
}
]
}
There is a json that represents directory structure.有一个代表目录结构的 json。 All "file" property in json has absolute path assigned to property "path".
json 中的所有“文件”属性都具有分配给属性“路径”的绝对路径。 But each sub-directories/directories is missing "path" value.
但是每个子目录/目录都缺少“路径”值。 Each subdirectories which have path=null needs to be assigned path on the basis of deepest child(type="file") which has absolute path defined.(Expected output is commented as //should be calculated as: )
每个 path=null 的子目录需要根据定义了绝对路径的最深子目录(type="file")分配路径。(预期 output 注释为//should becalculated as: )
I tried iterative approach, but problem is I have to traverse json from depth to top(ie. deepest children towards parent).我尝试了迭代方法,但问题是我必须从深度到顶部遍历 json(即最深的孩子朝向父母)。 Can someone recommend cleaner approach?
有人可以推荐更清洁的方法吗?
You could set path
by taking name
property and iterate children
, if exist and hand over the last path.您可以通过获取
name
属性并迭代children
来设置path
(如果存在并交出最后一条路径)。
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; }
You can try this,你可以试试这个
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))
}
}
call this function with the object and path name as '/' eg setPath(obj,'/')
调用此 function 并使用 object 和路径名称为 '/' 例如
setPath(obj,'/')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.