我JSON對象的數組是這樣的:創建一個嵌套的JSON對象
var fileArray = [
{ name: "file1", path: "/main/file1" },
{ name: "file2", path: "/main/folder2/file2" },
{ name: "file4", path: "/main/folder3/file4" },
{ name: "file5", path: "/main/file5" },
{ name: "file6", path: "/main/file6" }
];
我希望它看起來像最終是:
fileTree = [
{
"name": "file1",
"children": []
},
{
"name": "folder1"
"children": [
{
"name": "folder2",
"children": [
{
"name": "file2",
"children": []
}
]
},
{
"name": "folder3",
"children": [
{
"name": "file4",
"children": []
}
]
}
]
},
{
"name": "file5",
"children": []
},
{
"name": "file6",
"children": []
}
];
我嘗試了Create a nested UL menu based on the URL path structure of menu items中提到的解決方案,但對第一個答案的第一條評論正是我遇到的問題。所有的幫助表示讚賞。
在哪裏呢文件夾1從何而來,也就是爲什麼會一個文件有一個兒童屬性,雖然是空的。 – James
_I有一個JSON對象array_不是如果它看起來像你不 – George
爲什麼你不包括你在這個問題中嘗試過的鏈接片段,並演示不起作用的問題/邊界情況?確切地說,有人會爲你編寫代碼,但我不確定你是否會從中學到很多... – user3297291