Build javascript tree based on parent attribute in element array

I am trying to save a directory tree in mongoDB. Here is my diagram:

{ "_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") } 

I want to change this so that the children are inserted inside the parents. i.e:.

 [ { "_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 } 

Here is the function that I use to try to execute this:

 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; } }; 

I call the function as follows:

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

My console returns this:

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

Why are children undefined?

Here's a live demo: http://meteorpad.com/pad/imyw4w7z7dihss9t3

+6
source share
1 answer

You want to use _.map instead of _.each , see here .

_.map returns an array with the results of your iteration function that you want to assign to the children parameter. _.each just loops through your collection, returning undefined .

+2
source

Source: https://habr.com/ru/post/975546/


All Articles