0

I am trying to create a treeview from a series of AWS S3 results. I need to create an array in a specific Key:Value format so that we can use them dynamically in our website. I have looked at many different solutions, but couldn't find one that solved this problem. Using Node for this app, so will be using Javascript.

This has to be dynamic as the depth of the Buckets can vary widely.

Source Array:

[{  
    Key: "Test Folder/",
},
{
    Key: "Test Folder/2nd level folder/",
},
{
    Key: "Test Folder/2nd level folder/West-Rockies Flow Summary 191204.pdf",
},
{
    Key: "Test Folder/2nd level folder/West-Rockies Flow Summary 191205.pdf",
},
{
    Key: "Test Folder/2nd level folder/sub-sub-folder/",
},
{
    Key: "Test Folder/2nd level folder/sub-sub-folder/West-Rockies Flow Summary 191120.pdf",
},
{
    Key: "Test Folder/2nd level folder/sub-sub-folder/West-Rockies Flow Summary 191121.pdf",
},
{
    Key: "Test Folder/West-Rockies Flow Summary 191211.pdf",
},
{
    Key: "Test Folder/West-Rockies Flow Summary 191212.pdf",
},
{
    Key: "main/",
},
{
    Key: "main/test.txt",
}

]

Required Output:

[{
name: "Test Folder",
folders : [{
    name : "2nd level folder",
    folders : [{
        name : "sub-sub-folder",
        folders : [],
        files : [{
            Key : "Test Folder/2nd level folder/sub-sub-folder/West-Rockies Flow Summary 191120.pdf"
        },{
            Key : "Test Folder/2nd level folder/sub-sub-folder/West-Rockies Flow Summary 191121.pdf"
        }]
    }],
    files : [{
        Key : "Test Folder/2nd level folder/West-Rockies Flow Summary 191204.pdf"
    }, {
        Key : "Test Folder/2nd level folder/West-Rockies Flow Summary 191205.pdf"
    }]
}],
files : [{
    Key : "Test Folder/West-Rockies Flow Summary 191211.pdf"
}, {
    Key : "Test Folder/West-Rockies Flow Summary 191212.pdf"
}]
},
{
    name : "main",
    folders : [],
    files : [{
        Key : "main/test.txt"
    }]
}];

0 Answers0