根据元素数组中的父属性构建javascript树

时间:2014-09-19 16:29:16

标签: javascript mongodb recursion meteor underscore.js

我试图将目录树存储在mongoDB中。这是我的架构:

{ "_id" : ObjectId("541ba7f156d876d3f787bc33"), "name" : "file_1.mp3", "length" : 136.6, "kind" : "audio", "parent" : null }
{ "_id" : ObjectId("541ba7f156d876d3f787bc34"), "name" : "file_2.mp3", "length" : 132.0, "kind" : "audio", "parent" : null }
{ "_id" : ObjectId("541ba7f156d876d3f787bc35"), "name" : "file_3.mp3", "length" : 116.8, "kind" : "audio", "parent" : null }
{ "_id" : ObjectId("541ba7f156d876d3f787bc36"), "name" : "file_4.mp3", "length" : 206.7, "kind" : "audio", "parent" : null }
{ "_id" : ObjectId("541ba84456d876d3f787bc37"), "name" : "folder_1", "length" : null, "kind" : "dir", "parent" : null }
{ "_id" : ObjectId("541ba84456d876d3f787bc38"), "name" : "folder_2", "length" : null, "kind" : "dir", "parent" : null }
{ "_id" : ObjectId("541ba84456d876d3f787bc39"), "name" : "folder_3", "length" : null, "kind" : "dir", "parent" : null }
{ "_id" : ObjectId("541ba91656d876d3f787bc3a"), "name" : "subfolder_1", "length" : null, "kind" : "dir", "parent" : ObjectId("541ba84456d876d3f787bc37") }
{ "_id" : ObjectId("541ba91656d876d3f787bc3b"), "name" : "subfolder_2", "length" : null, "kind" : "dir", "parent" : ObjectId("541ba84456d876d3f787bc37") }
{ "_id" : ObjectId("541ba91656d876d3f787bc3c"), "name" : "subfolder_3", "length" : null, "kind" : "dir", "parent" : ObjectId("541ba84456d876d3f787bc37") }
{ "_id" : ObjectId("541ba98056d876d3f787bc3d"), "name" : "subsubfolder_1", "length" : null, "kind" : "dir", "parent" : ObjectId("541ba91656d876d3f787bc3b") }
{ "_id" : ObjectId("541ba98056d876d3f787bc3e"), "name" : "subsubfolder_2", "length" : null, "kind" : "dir", "parent" : ObjectId("541ba91656d876d3f787bc3b") }
{ "_id" : ObjectId("541ba98056d876d3f787bc3f"), "name" : "subsubfolder_3", "length" : null, "kind" : "dir", "parent" : ObjectId("541ba91656d876d3f787bc3b") }

我想改变它,以便孩子们嵌套在父母中。即:

[
  { 
    "_id" : ObjectId("541ba7f156d876d3f787bc33"),
    "name" : "file_1.mp3",
    "length" : 136.6,
    "kind" : "audio",
    "parent" : null
  }
  { 
    "_id" : ObjectId("541ba7f156d876d3f787bc34"),
    "name" : "file_2.mp3",
    "length" : 136.6,
    "kind" : "audio",
    "parent" : null
  }
  {
    "_id" : ObjectId("541ba7f156d876d3f787bc35"),
    "name" : "file_3.mp3",
    "length" : 136.6,
    "kind" : "audio",
    "parent" : null
  }
  {
    "_id" : ObjectId("541ba7f156d876d3f787bc36"),
    "name" : "file_4.mp3",
    "length" : 136.6,
    "kind" : "audio",
    "parent" : null
  }
  {
    "_id" : ObjectId("541ba84456d876d3f787bc37"),
    "name" : "folder_1",
    "length" : null,
    "kind" : "dir",
    "parent" : null
    "children": [
      {
        "_id" : ObjectId("541ba91656d876d3f787bc3a"),
        "name" : "subfolder_1",
        "length" : null,
        "kind" : "dir",
        "parent" : ObjectId("541ba84456d876d3f787bc37")
      },
      {
        "_id" : ObjectId("541ba91656d876d3f787bc3b"),
        "name" : "subfolder_2",
        "length" : null,
        "kind" : "dir",
        "parent" : ObjectId("541ba84456d876d3f787bc37")
        "children": [
          {
            "_id" : ObjectId("54198056d876d3f787bc3d"),
            "name" : "subsubfolder_1",
            "length" : null,
            "kind" : "dir",
            "parent" : ObjectId("541ba91656d876d3f787bc3b")
          }
          {
            "_id" : ObjectId("541ba98056d876d3f787bc3e"),
            "name" : "subsubfolder_2",
            "length" : null,
            "kind" : "dir",
            "parent" : ObjectId("541ba91656d876d3f787bc3b")
          }
          {
            "_id" : ObjectId("541ba98056d876d3f787bc3f"),
            "name" : "subsubfolder_3",
            "length" : null,
            "kind" : "dir",
            "parent" : ObjectId("541ba91656d876d3f787bc3b")
          }
        ]
      },
      {
        "_id" : ObjectId("541ba91656d876d3f787bc3c"),
        "name" : "subfolder_3",
        "length" : null,
        "kind" : "dir",
        "parent" : ObjectId("541ba84456d876d3f787bc37")
      }
    ]
  }
  {
    "_id" : ObjectId("541ba84456d876d3f787bc38"),
    "name" : "folder_2",
    "length" : null,
    "kind" : "dir",
    "parent" : null
  }
  {
    "_id" : ObjectId("541ba84456d876d3f787bc39"),
    "name" : "folder_3",
    "length" : null,
    "kind" : "dir",
    "parent" : null
  }

这是我用来尝试完成此任务的功能:

getChildren = function(element) {
  // find all the entries that have this element as their parent
  var results = FileTree.find({'parent': element._id});

  // did we find any children?
  if (results.count() !== 0) {
    // get an array from the mongo cursor
    var rArr = results.fetch();
    // attach a '.children' object to each of the children
    // so that we continue the tree
    var children = _.each(rArr, getChildren);
    //make this subtree part of the original element
    element.children = children;
    return element;
  }
  else { // the element doesn't have any children
     element.children = null;
     return element;
  }
};

我这样称呼函数:

id = new Meteor.Collection.ObjectID("541ba84456d876d3f787bc37");
getChildren(FileTree.findOne({'_id': id}));

我的控制台会返回:

Object { _id: Object, name: "folder_1", length: null, kind: "dir", parent: null, children: undefined }

为什么儿童未定义?

这是一个现场演示:http://meteorpad.com/pad/imyw4w7z7dihss9t3

1 个答案:

答案 0 :(得分:2)

您想使用_.map代替_.each,请参阅here

_.map返回包含迭代函数结果的数组,这是您要分配给children参数的结果。 _.each只会循环播放您的收藏集,并返回undefined