3

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 this
const 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.

5
  • 3
    TBH, the first structure seems better than the second. Why do you want to repeat the parent paths everywhere? Commented May 2, 2019 at 18:42
  • 3
    ... totally agree. I'd actually prefer a tree of objects here, e.g. { name: "home", childPaths: [...], files: [...] } Commented May 2, 2019 at 18:46
  • Absolutely. Unfortunately, this is the structure needs to be handled. I know correct way to construct a tree in Javascript is using an object. Commented May 2, 2019 at 19:08
  • 1
    this is an awful structure to work with [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? Commented May 3, 2019 at 9:17
  • Yeah. Actually, I am working with this structure as homework. If I receive similar data like this in real life, probably I resist working with it. Unless I insist on my opinion about creating a new clean tree from this structure for one time and go towards with it. Leave this terrible data for the dead. :) Commented May 3, 2019 at 14:09

1 Answer 1

3

You could iterate the arrays and take non arrays as path and store this for nested arrays.

function getArrays(array, path = '') {
    var temp;
    return array.map(v => Array.isArray(v)
        ? getArrays(v, temp)
        : (temp = path + (path && '/') + v)
    );
}

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"]]]]]];

console.log(getArrays(directories));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.