Skip to content

Instantly share code, notes, and snippets.

@KrishnaPravin
Created August 25, 2021 06:56
Show Gist options
  • Save KrishnaPravin/c7544a349de220c0d0b52c4414eead2c to your computer and use it in GitHub Desktop.
Save KrishnaPravin/c7544a349de220c0d0b52c4414eead2c to your computer and use it in GitHub Desktop.
convert file path in object format to array structure suitable for rendering file tree
const k = {
Age1036: {
FGB05: {
'FBG05 06hr 2020': {
'Section 1': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
'Section 2': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
},
'FBG05 07hr 2020': {
'Section 1': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
'Section 2': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
},
},
FGB06: {
'FBG06 06hr 2020': {
'Section 1': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
'Section 2': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
},
'FBG06 07hr 2020': {
'Section 1': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
'Section 2': [
'file_example_TIFF_1MB - Copy (2).tiff',
'file_example_TIFF_1MB - Copy (3).tiff',
'file_example_TIFF_1MB - Copy (4).tiff',
],
},
},
},
};
function getChildren(pathMap, parentPath = '') {
return Object.entries(pathMap).reduce((acc, [pathName, children]) => {
const currentDir = {value: parentPath + '/' + pathName, label: pathName};
if (Array.isArray(children)) {
currentDir.children = children.map((fileName) => ({value: parentPath + '/' + fileName, label: fileName}));
} else {
currentDir.children = getChildren(children, parentPath + '/' + pathName);
}
acc.push(currentDir);
return acc;
}, []);
}
l = getChildren(k);
m = JSON.stringify(l, null, 2);
m;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment