[英]JavaScript: Can a nested object value reference its parent object key?
[英]Get parent object from key or its value in a nested object
我有一個 object,其中包含有關目錄的文件夾和文件的所有信息以分層方式。
例如
{
"path": "./parent",
"name": "parent",
"type": "folder",
"children": [
{
"path": "./parent/child1",
"name": "child1",
"type": "folder",
"children": [
{
"path": "./parent/child1/file1",
"name": "file1",
"size": 651956838,
"extension": ".pdf",
"type": "file"
},
{
"path": "./parent/child1/file2",
"name": "file2",
"size": 468327031,
"extension": ".pdf",
"type": "file"
}
]
},
{
"path": "./parent/child2",
"name": "child2",
"type": "folder",
"children": [
{
"path": "./parent/child2/file3",
"name": "file1",
"size": 651956838,
"extension": ".pdf",
"type": "file"
},
{
"path": "./parent/child2/file4",
"name": "file2",
"size": 468327031,
"extension": ".pdf",
"type": "file"
}
]
}
]
}
[ { "path": "./parent/child1/file1", "name": "file1", "size": 651956838, "extension": ".pdf", "type": "file" }, { "path": "./parent/child1/file2", "name": "file2", "size": 468327031, "extension": ".pdf", "type": "file" } ]
所以我想知道這是否可能。 如果是,那么如何,如果不是,那么還有其他方法可以達到類似的結果嗎?
這可以使用遞歸 function 來完成。
const paths = {
"path": "./parent",
"name": "parent",
"type": "folder",
"children": [
{
"path": "./parent/child1",
"name": "child1",
"type": "folder",
"children": [
{
"path": "./parent/child1/file1",
"name": "file1",
"size": 651956838,
"extension": ".pdf",
"type": "file"
},
{
"path": "./parent/child1/file2",
"name": "file2",
"size": 468327031,
"extension": ".pdf",
"type": "file"
}
]
},
{
"path": "./parent/child2",
"name": "child2",
"type": "folder",
"children": [
{
"path": "./parent/child2/file3",
"name": "file1",
"size": 651956838,
"extension": ".pdf",
"type": "file"
},
{
"path": "./parent/child2/file4",
"name": "file2",
"size": 468327031,
"extension": ".pdf",
"type": "file"
}
]
}
]
}
const pathtofind = "./parent/child1";
function findChildrenInPath(object, path) {
if (path.startsWith(object.path)) {
if (object.path == path) {
return object.children;
}
else {
for (let child of object.children) {
const result = findChildrenInPath(child, path);
if (result) {
return result;
}
}
}
}
}
const res = findChildrenInPath(paths, pathtofind);
console.log(res);
這是另一種方法,沒有遞歸:
const data={ "path": "./parent", "name": "parent", "type": "folder", "children": [ { "path": "./parent/child1", "name": "child1", "type": "folder", "children": [ { "path": "./parent/child1/file1", "name": "file1", "size": 651956838, "extension": ".pdf", "type": "file" }, { "path": "./parent/child1/file2", "name": "file2", "size": 468327031, "extension": ".pdf", "type": "file" } ] }, { "path": "./parent/child2", "name": "child2", "type": "folder", "children": [ { "path": "./parent/child2/file3", "name": "file1", "size": 651956838, "extension": ".pdf", "type": "file" }, { "path": "./parent/child2/file4", "name": "file2", "size": 468327031, "extension": ".pdf", "type": "file" } ] } ] }; function getChildrenOf(fs,pth){ let ptha=pth.split('/'); ptha.forEach((d,i,a)=>a[i]=(i?a[i-1]+'/':'')+d); // console.log(ptha); return ptha.filter(d=>d.=="."),reduce((ac)=> a=a.find(d=>d.path===c && d.type==="folder"),children;[fs]). } console,log(getChildrenOf(data.";/parent/child1"));
由於data
數組不包含有關當前目錄 ( ./
) 的任何信息,因此我從ptha
數組中刪除了該元素。 否則,搜索將允許在任意深度的文件結構中進行搜索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.