1

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;
}
6
  • 1.) Are you trying to get all the files? 2.) Why are you trying to do this? Commented Jul 25, 2014 at 17:21
  • Also, you are trying to extract values from an object -- not an Array. Be precise. :) Commented Jul 25, 2014 at 17:22
  • yes. All the filenames. Commented Jul 25, 2014 at 17:22
  • "2.) Why are you trying to do this?" Smells like homework to me. Commented Jul 25, 2014 at 17:22
  • Also, your DirectoryTree doesn't have valid syntax according to JSFiddle. :P Commented Jul 25, 2014 at 17:25

2 Answers 2

2

You can do this recursively, like this

function getFiles(currentObject, result) {
    var type = Object.prototype.toString.call(currentObject),
        idx;
    if (type === "[object Object]") {
        for (idx in currentObject) {
            if (currentObject.hasOwnProperty(idx) && idx === "files") {
                currentObject[idx].forEach(function(object) {
                    getFiles(object, result);
                });
            }
        }
    } else if (type === "[object Array]") {
        currentObject.forEach(function(object) {
            getFiles(object, result);
        });
    } else if (type === "[object String]") {
        result.push(currentObject);
    }
    return result;
}

console.log(getFiles(DirectoryTree, []))

Output

[ 'proj.config',
  'name.htm',
  'dept.htm',
  'salary.htm',
  'name.js',
  'dept.js',
  'salary.js',
  'proj.config',
  'name.htm',
  'dept.htm',
  'salary.htm',
  'name.js',
  'dept.js',
  'salary.js' ]
Sign up to request clarification or add additional context in comments.

Comments

0

I answered a similar question today and solved it using a few underscore functions.
The format of your example is pretty weird, but I would advise again to use a recursive function for this type of reads:

function recursiveFuntion(collection){
    collection.forEach(function(model) { 
        //console.log(model.files); --> Or do whatever you need 
        if(model.files.length > 0){ 
            recursiveFunction(model); 
        }; 
     }); 
}; 

recursiveFuntion(DirectoryTree.files); 

The benefit of the recursive function is that it is dynamic.
As you are not working with an array but with an object, you'll need to customise this code.
But this is the basic principle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.