I want to convert these pieces of code into smaller one via recursion, but I stuck in place where I'm using for loop.
I have the next dictionary: var structure = []; with the structure:
"path": path, "children": [] I'm filling it by parsing my JSON file. One of the paths from the JSON looks like: "path": "Assignment_1/src/com",, so I'm parsing path by / and trying to rebuild this structure inside of my structure dictionary. The first part, "path": "Assignment_1/", I'm putting inside of my structure. The second part "path": "Assignment_1/src/", I'm putting inside of the children dictionary and so on.
Without recursion I'm doing this:
if(path.split("/").length == 2) { if(type == "tree") { var path0 = path.split("/")[0]; var path1 = path.split("/")[1]; for(var j = 0; j < structure.length; j++) { var foundPath = structure[j]["path"]; if(foundPath == path0) { structure[j]["children"].push({ "path": path1, "children": [] }) } } } } if(path.split("/").length == 3) { if(type == "tree") { var path0 = path.split("/")[0]; var path1 = path.split("/")[1]; var path2 = path.split("/")[2]; for(var j = 0; j < structure.length; j++) { var foundPath = structure[j]["path"]; if(foundPath == path0) { for(var k = 0; k < structure[j]["children"].length; k++) { var foundPath = structure[j]["children"][k]["path"]; if(foundPath == path1) { structure[j]["children"][k]["children"].push({ "path": path2, "children": [] }) } } } print(structure); } } } Now I want to unify it, so it will automatically go through all folders and fill my structure dictionary. I started with while loops, but this parts:
structure[j]["children"].push({ }) structure[j]["children"][k]["children"].push({ }) are too difficult to program. Any help or advice will help me a lot!
UPDATE
Input is(one part):
{ "path": "Folder_1/src/com", "mode": "040000", "type": "tree" }, Output:

reducecan help.path.split("/")over and over again. Call it once and save the result.