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