Loop to a filesystem structure in my object to get all the files

后端 未结 4 1444
孤独总比滥情好
孤独总比滥情好 2021-01-17 01:16

I get a Javascript Object from my server, which depicts a filesystem. Now I want to get the path of all files in the system, e.g. the endpoints of the tree.

File str

4条回答
  •  一整个雨季
    2021-01-17 01:34

    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":[]}]}]};
    
    function getPaths(obj){
        let foundPaths = [];
        if(obj.children.length > 0){
            obj.children.forEach(function (element){
               let childPaths = getPaths(element);
               foundPaths = foundPaths.concat(childPaths);
            });
            return foundPaths;
       } else {
          foundPaths.push(obj.path);
          return foundPaths;
       }
    }
    
    let paths = getPaths(json);
    
    document.getElementById('output').innerHTML += paths.join("\n");

提交回复
热议问题