【发布时间】: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