javascriptnode.jsmongodbnested-sets

Tree traversal in javascript


I am trying to generate URLs for pages stored in a MongoDB in node.

Using the following function I want to traverse a javascript object that and display the path to each element.

I am nearly there, but I am stuck - There might even be a better way to do this using using Async (which I must admit, confuses me a bit).

Function: (demo)

function printTree(people, slug) {
    for (var p = 0; p < people.length; p++) {
        var root = people[p];
        slug = slug + root.name + "/";
        console.log(slug);
        if (root.children.length > 0) {
            var childrenCount = root.children.length;
            for (var c = 0; c < childrenCount; c++) {
                if (root.children[c].children.length > 0) {
                    printTree(root.children[c].children, slug + root.children[c].name + "/");
                }
            }
        }
    }
};

Output:

/michael/
/michael/angela/oscar
/michael/meredith/creed
/michael/meredith/creed/kelly

Expected Output:

/michael/
/michael/angela/
/michael/angela/oscar/
/michael/meredith/
/michael/meredith/creed/
/michael/meredith/kelly/

Object:

[
  {
    "name": "michael",
    ...
    "children": [
      {
        "name": "angela",
        ...
        "children": [
          {
            "name": "oscar",
            ...
            "children": []
          }
        ]
      },
      {
        "name": "meredith",
        ...
        "children": [
          {
            "name": "creed",
            ...
            "children": []
          },
          {
            "name": "kelly",
            ...
            "children": []
          }
        ]
      },
      { ... }
    ]
  }
]

If it helps, the data is stored using nested sets: https://github.com/groupdock/mongoose-nested-set So there might be a better way to do the above work using nested sets (negating the above object).


Solution

  • Here you go. You don't need a second for loop, since your printTree function is going to loop through everything anyway (demo).

    function printTree(people, slug){
      slug = slug || '/';
      for(var i = 0; i < people.length; i++) {
        console.log(slug + people[i].name + '/');
        if(people[i].children.length){
          printTree(people[i].children, slug + people[i].name + '/')
        }
      }
    }