There is an Array which inspired from the hash tree object. But the structure is not well-designed and little bit complicated.
const directories = [
"/main",
[
"folder",
["subFolder", ["directory1", "directory2", "directory3"]],
"folder2",
["subFolder", ["directory4", "directory5"]],
"folder3",
[
"subFolder",
["directory4", "directory5", "directory6", "directory7"],
"subFolderWrapper",
["folder1", ["subFolder", ["child1", "child2", "child3", "child4"]]]
]
]
]
A recursive function must be created and returned a new array based on the nested relations from the given.
something like thisconst result = [
"/main/",
[
"/main/folder",
[
"/main/folder/subFolder",
[
"/main/folder/subFolder/directory1",
"/main/folder/subFolder/directory2",
"/main/folder/subFolder/directory3"
]
],
"/main/folder2",
[
"/main/folder2/subFolder",
[
"/main/folder2/subFolder/directory4",
"/main/folder2/subFolder/directory5",
"/main/folder2/subFolder/directory6",
"/main/folder2/subFolder/directory7"
]
],
"/main/folder3",
[
"/main/folder3/subFolder",
[
"/main/folder3/subFolder/directory4",
"/main/folder3/subFolder/directory5",
"/main/folder3/subFolder/directory6",
"/main/folder3/subFolder/directory7"
],
"/main/folder3/subs",
[
"/main/folder3/subFolderWrapper/folder1",
[
"/main/folder3/subs/folder1/subFolder",
[
"/main/folder3/subs/folder1/subFolder/directory1",
"/main/folder3/subs/folder1/subFolder/directory2",
"/main/folder3/subs/folder1/subFolder/directory3",
"/main/folder3/subs/folder1/subFolder/directory4"
]
]
]
]
]
];
I tried different kinds of logic in this function below but it's kind of different tree implementation that I didn't saw before. It looks like it's some kinds of cheating needs to be applied. Because there are two kinds of arrays that I know. One of them is flat also known as single dimensional and the other one is a two-dimensional array.
function traverse(item) {
for(let index in item){
if (Array.isArray(item[index])) {
// logic for creating nested array
traverse(item[index]);
}
else {
// logic for non array strings
}
}
}
Counted the nested invocation how much time the recursive function occurs. Subtracted or added to depthCounter variable from the current index of item for reaching and visiting the next node like BFS did.
I am curious about what is the best way of achieving this process.
{ name: "home", childPaths: [...], files: [...] }[nr1name, nr1children, nr2name, nr2children, nr3name, nr4name, nr5name, nr5children, nr6name, ...]. I understand that this is the data format you get in, but do you really have to use it?