I have a JSON object list that needs to be transformed into a specific structure.
var files = [{name: "1.xml", path: "folder1/1.xml", tag: "folder1", type: "file"},
{name: "2.jpg", path: "folder1/folder2/2.jpg", type: "file", tag: "folder1/folder2"},
{name: "3.doc", path: "folder1/folder2/3.doc", type: "file", tag: "folder1/folder2"}];
I am looking to create the following hierarchical structure:
[
{
name: "folder1",
path: "folder1",
type: "folder",
items:[
{name: "folder2",
path: "folder1/folder2",
type: "folder",
items:[
{name: "2.xml", path: "folder1/folder2/2.xml", type: "file"},
{name: "3.xml", path: "folder1/folder2/3.xml", type: "file"}
]},
{name: "1.xml", path: "folder1/1.xml", type: "file"}
]}]
The goal is to convert the 'tag' property of each file into a folder, grouping related files and other tags into an 'items' collection.