【问题标题】:How to write the code recursively?如何递归编写代码?
【发布时间】:2017-12-12 16:01:37
【问题描述】:

我想通过递归将这些代码段转换成更小的代码段,但我卡在了使用for 循环的地方。

我有下一本字典:var structure = [];,结构如下:

"path": path,
"children": []

我通过解析我的 JSON 文件来填充它。 JSON 的其中一条路径如下所示:"path": "Assignment_1/src/com",,因此我正在通过/ 解析路径并尝试在我的structure 字典中重建此结构。第一部分,"path": "Assignment_1/", 我正在放入我的结构中。第二部分"path": "Assignment_1/src/",我放在children字典里面等等。

没有递归我这样做:

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);
            }
        }
    }

现在我想统一它,所以它会自动遍历所有文件夹并填充我的structure 字典。我从 while 循环开始,但这部分:

structure[j]["children"].push({ })
structure[j]["children"][k]["children"].push({ })

太难编程了。任何帮助或建议都会对我有很大帮助!

更新

输入是(一部分):

{
  "path": "Folder_1/src/com",
  "mode": "040000",
  "type": "tree"
},

输出:

【问题讨论】:

  • 使用reduce 可以提供帮助。
  • @j.doe 请提供示例输入/输出
  • @Jonasw 我已经添加了。我希望这会有所帮助。如果您需要什么,请尽管问我!
  • 请不要一遍又一遍地打电话给path.split("/")。调用一次并保存结果。
  • Create nested object from multiple string paths 的可能副本 name 更改为 path 即可...

标签: javascript arrays json dictionary recursion


【解决方案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

虽然我没有考虑到type == tree,不管那是什么。

【讨论】:

  • 感谢您的回答!它可以工作,但它会将一个文件夹多次推送到数组中,例如 Folder1、Folder1/children、Folder1/children/children,而不仅仅是最终结果。我现在正在尝试修复它
  • 您能帮我删除重复项并加入同一父级的文件夹吗?
  • 现在看来它只需要第一个文件夹,没有子文件夹:)
  • 它现在似乎工作错了。它只放置主(父)文件夹,没有子文件夹=/
  • @J.Doe 怎么会这样?你看看我的 jfiddle 了吗?它对我来说很好用!
猜你喜欢
  • 1970-01-01
  • 2016-09-22
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多