我一直在尝试使用将路径数组转换为JSON
父子树node js
。
我正在使用我稍微修改过的提到的答案。下面是我的代码:
function buildTree(obj) {
let result = [];
let level = {
result
};
obj.forEach(item => {
if (typeof item.fsLocation != "undefined") {
var obj = {}
var path = ""
item.fsLocation.split('/').reduce((r, name, i, a) => {
path += "/"+name
if (!r[name]) {
r[name] = {
result:[]
};
obj = {
name,
children: r[name].result
}
if(r[name].result.length < 1){
obj["path"] = item.fsLocation
obj["fileSize"] = item.fileSize
obj["createDate"] = item.createDate
obj["editDate"] = item.editDate
obj["fileType"] = item.fileType
obj["version"] = item.version
}
r.result.push(obj)
}
return r[name];
}, level)
}
})
return result
}
obj:
[
{
"createDate":"2019-10-03T07:00:00Z",
"fileType":"pptx",
"fsLocation":"Events/Plays/Technologies/Continuity/technology.pptx",
"fileSize":46845322,
"fileName":"technology.pptx",
"editDate":"2019-10-03T07:00:00Z",
"version":"10.0"
},
{
"fileName":"operations.pptx",
"fileSize":23642178,
"fileType":"pptx",
"fsLocation":"Events/Plays/Technologies/operations.pptx",
"createDate":"2019-01-08T08:00:00Z",
"editDate":"2019-01-09T08:00:00Z",
"version":"15.0"
},
{
"fileName":"Solution.pdf",
"createDate":"2016-06-16T22:42:16Z",
"fileSize":275138,
"fsLocation":"Events/Plays/Technologies/Solution.pdf",
"fileType":"pdf",
"editDate":"2016-06-16T22:42:16Z",
"version":"1.0"
}
]
知道如何产生上述输出吗?
芜湖不芜
相关分类