Print Directory Structure

Print Directory Structure saved in JS object.

by MikeGrace

JavaScript

var structure = {
    "feed": {},
    "posts": {
        "2011": {
            "jan": {
                "4": {}
            }
        }
    }
};

var printDirectory = function(obj, depth) {
    if(!depth) {depth=0;}
    for(var folder in obj) {
        console.log(spaces(depth)+"/"+folder);
        if(Object.keys(obj[folder]).length) {
            printDirectory(obj[folder], depth+1);
        }
    } 
};

var spaces = function(depth) {
    var s = [];
    for(var i=0; i<depth*2; i++) {
        s.push(" ");   
    }
    return s.join("");
};

printDirectory(structure);