javascript - 循环到我的对象中的文件系统结构以获取所有文件

标签 javascript recursion

我从我的服务器获取一个 Javascript 对象,它描述了一个文件系统。现在我想获取系统中所有文件的路径,例如树的端点。

文件结构示例:

└── pages
    └── services
        └── project            
            │── headline
            │── test
            │   └── text
            │   └── picture 
            │── text

可读的 JSON:

{
"path":"/pages/services/project",
"is_dir":true,
"children":[
  {
     "path":"/pages/services/project/headline",
     "is_dir":false,
     "children":[

     ]
  },
  {
     "path":"/pages/services/project/text",
     "is_dir":false,
     "children":[

     ]
  },
  {
     "path":"/pages/services/project/test/",
     "is_dir":true,
     "children":[
        {
           "path":"/pages/services/project/test/text",
           "is_dir":false,
           "children":[

           ]
        },
        {
           "path":"/pages/services/project/test/picture",
           "is_dir":false,
           "children":[

           ]
        }
     ]
  }

] }

预期输出:

/pages/services/project/headline
/pages/services/project/text
/pages/services/project/test/text
/pages/services/project/test/picture

我玩了一下递归,并制作了一个愚蠢的函数,当目录只有一个子目录时该函数可以工作。我的问题是我无法掌握照顾更多 child 的方法。有没有办法迭代每个 child ?

这是我的代码:

var json = {"path":"/pages/services/project", "is_dir":true, "children":[{"path":"/pages/services/project/headline","is_dir":false,"children":[]},{"path":"/pages/services/project/text","is_dir":false,"children":[]},
{"path":"/pages/services/project/test/","is_dir":true,"children":[{"path":"/pages/services/project/test/text","is_dir":false,"children":[]},
{"path":"/pages/services/project/test/picture","is_dir":false,"children":[]}]}]};

json.children.forEach(function (child) {
	out(goToDeepestPoint(child).path);
});



function goToDeepestPoint(node) {
    if (node.is_dir)
        return goToDeepestPoint(node.children[0]);
    else 
        return node;
}

function out()
{
    var args = Array.prototype.slice.call(arguments, 0);
    document.getElementById('output').innerHTML += args.join(" ") + "\n";
}
<pre id="output"></pre>

最佳答案

工作解决方案:

var json = {"path":"/pages/services/project", "is_dir":true, "children":[{"path":"/pages/services/project/headline","is_dir":false,"children":[]},{"path":"/pages/services/project/text","is_dir":false,"children":[]},
{"path":"/pages/services/project/test/","is_dir":true,"children":[{"path":"/pages/services/project/test/text","is_dir":false,"children":[]},
{"path":"/pages/services/project/test/picture","is_dir":false,"children":[]}]}]};

json.children.forEach(function (child) {
    goToDeepestPoint(child);
});



function goToDeepestPoint(node) {
    if (node.is_dir){
      for(var i=0;i<node.children.length;i++){
        goToDeepestPoint(node.children[i]);
      }
    }        
    else {
        out(node.path);
    }
}

function out()
{
    var args = Array.prototype.slice.call(arguments, 0);
    document.getElementById('output').innerHTML += args.join(" ") + "\n";
}

关于javascript - 循环到我的对象中的文件系统结构以获取所有文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45565900/

相关文章:

javascript - 递归数组修改

javascript - Firefox、Chrome、Safari、IE 等的 js 递归限制是什么?

javascript - 使用 JS 单击跨度时更改跨度的 onClick

Java - 复杂的递归回溯

javascript - 将 AngularJS 与 KineticJS 一起使用

javascript - 如何显示用户选择上传的图像

javascript - 如何将 polymer 纸标签对齐到右侧

javascript - 使用映射或模板转换 Node 流中的 JSON

javascript - 添加变量名称而不是其值,javascript

c# - Console.WriteLine 出于某种原因重复自身