-1

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:

enter image description here

5
  • using reduce can help. Commented Jul 8, 2017 at 12:36
  • 2
    @j.doe please provide sample input/output Commented Jul 8, 2017 at 12:39
  • @Jonasw I've added. I hope that will help. If do you need anything, just ask me, please! Commented Jul 8, 2017 at 12:46
  • Please don't call path.split("/") over and over again. Call it once and save the result. Commented Jul 8, 2017 at 12:52
  • Possible duplicate of Create nested object from multiple string paths <= just change name to path and youre done... Commented Jul 8, 2017 at 12:53

1 Answer 1

1
var inputs = [ { "path": "Folder_1/src/com", "mode": "040000", "type":"tree" }, { "path": "Folder_1/src/com", "mode": "040000", "type":"tree" }, { "path": "Folder_2/docs/files", "mode": "040000", "type":"tree" } ], output = []; inputs.forEach( function( input ) { parse( input.path.split('/'), output ); } ); function parse( input, into ){ var split = input, first = split.shift(), newItem = { 'src': first, 'children': [] }; if( split.length ){ parse( split, newItem.children ); } if( ! into.find(function(item){return item.src == first } ) ){ into.push( newItem ); } } console.log( output ); 

Jfiddle

Although I haven't taken into account type == tree, whatever that is.

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

5 Comments

thanks for your answer! It works, but it pushes one folder many times into the array, like Folder1, Folder1/children, Folder1/children/children, not only the final result. I'm trying to fix it now
could you please help me to remove duplicates and join folders with the same parent?
it seems now it takes only first folders, without children :)
it seems it works wrong now. It puts only the main(parent) folders, without children =/
@J.Doe how so? Did you check out my jfiddle? It's working fine for me!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.