I want to extract the files in this structure recursively. I have done it for the first level, but can't proceed further.
var DirectoryTree = {
dir: 'project1',
files: [
'proj.config', {
dir: 'src',
files: [
'name.htm',
'dept.htm',
'salary.htm', {
dir: 'scripts',
files: [
'name.js',
'dept.js',
'salary.js'
]
}
]
}, {
dir: 'project2',
files: [
'proj.config', {
dir: 'src',
files: [
'name.htm',
'dept.htm',
'salary.htm', {
dir: 'scripts',
files: [
'name.js',
'dept.js',
'salary.js'
]
}
]
}
]
}
]
};
Below is the code I have till now. I am kind of stuck here. Can you help me to get the data from the next levels.
function listFiles(dirTree, subFolder){
var fList=[];
if(dirTree.files){
for (var i=0;i<dirTree.files.length;i++){
if(typeof dirTree.files[i] === 'string'){
fList.push(dirTree.files[i]);
}
}
}
if(dirTree.dir){
return(listFiles(dirTree.dir, subFolder));
}
return fList;
}
Array. Be precise. :)DirectoryTreedoesn't have valid syntax according to JSFiddle. :P