gpt4 book ai didi

javascript - 基于元素数组中的父属性构建javascript树

转载 作者:IT老高 更新时间:2023-10-28 13:29:43 25 4
gpt4 key购买 nike

我正在尝试将目录树存储在 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") }

我想改变这一点,让 children 嵌套在 parent 身上。即:

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

为什么 child 没有定义?

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

最佳答案

您想使用 _.map 而不是 _.each,请参阅 here .

_.map 返回包含迭代函数结果的数组,这是您要分配给 children 参数的结果。 _.each 只是循环遍历你的集合,返回 undefined

关于javascript - 基于元素数组中的父属性构建javascript树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25938473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com