递归地重新构造对象javascript
因此,这是我获取与其相应主题相关的所有帖子的方法递归地重新构造对象javascript,javascript,mongodb,recursion,nested,Javascript,Mongodb,Recursion,Nested,因此,这是我获取与其相应主题相关的所有帖子的方法 const moment = require('moment'); const mongoose = require('mongoose'); mongoose.Promise = global.Promise; const Post = require('../models/Post'); let posts = { getPosts: function(req, res) { return Post.find({
const moment = require('moment');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const Post = require('../models/Post');
let posts = {
getPosts: function(req, res) {
return Post.find({ topicId: req.params._id })
.then(function(result) {
console.log('------------------------------------');
console.log('Headed to the Client: ', result);
console.log('------------------------------------');
res.json(result);
})
.catch(function(error) {
console.log('Nope! Nerd!');
return Promise.reject(error);
})
}
}
结果是这样的:
[ { _id: 58deac2223b5b92ce45bdfac,
topicId: '58dd6f541919c541dbf9632d',
parentId: null,
content: '1 Post',
author: 'Bob',
createdAt: 2017-03-31T19:21:06.698Z,
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: true },
{ _id: 58deac2c23b5b92ce45bdfad,
topicId: '58dd6f541919c541dbf9632d',
parentId: null,
content: '2 Post',
author: 'Bob',
createdAt: 2017-03-31T19:21:16.622Z,
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: true },
{ _id: 58deac3c23b5b92ce45bdfae,
topicId: '58dd6f541919c541dbf9632d',
parentId: null,
content: '3 Post',
author: 'Bob',
createdAt: 2017-03-31T19:21:32.682Z,
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: true },
{ _id: 58deac5e23b5b92ce45bdfaf,
topicId: '58dd6f541919c541dbf9632d',
parentId: '58deac3c23b5b92ce45bdfae',
content: '1 Comment',
author: 'Kat',
createdAt: 2017-03-31T19:22:06.974Z,
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: false },
{ _id: 58deac6c23b5b92ce45bdfb0,
topicId: '58dd6f541919c541dbf9632d',
parentId: '58deac2223b5b92ce45bdfac',
content: '2 Comment',
author: 'Dave',
createdAt: 2017-03-31T19:22:20.071Z,
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: false },
{ _id: 58deac7a23b5b92ce45bdfb1,
topicId: '58dd6f541919c541dbf9632d',
parentId: '58deac2c23b5b92ce45bdfad',
content: '4 Comment',
author: 'Bob',
createdAt: 2017-03-31T19:22:34.865Z,
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: false } ]
我被卡住了,因为我需要在这个列表从数据库中出来后修改它,以获取带有parentID的帖子,并将它们推送到相应的parents'comments数组中
因此,实际发送到客户端的结果如下所示:
[{
_id: 58deac2223b5b92ce45bdfa',
topicId: '58dd6f541919c541dbf9632d',
parentId: null,
content: '1 Post',
author: 'Bob',
isAnon: false,
__v: 0,
comments: [{
_id: 58deac6c23b5b92ce45bdfb0,
topicId: '58dd6f541919c541dbf9632d',
parentId: '58deac2223b5b92ce45bdfac',
content: '2 Comment',
author: 'Dave',
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: false
}],
isArchieved: false,
isParent: true
},
{
_id: 58deac2c23b5b92ce45bdfad,
topicId: '58dd6f541919c541dbf9632d',
parentId: null,
content: '2 Post',
author: 'Bob',
isAnon: false,
__v: 0,
comments: [{
_id: 58deac7a23b5b92ce45bdfb1,
topicId: '58dd6f541919c541dbf9632d',
parentId: '58deac2c23b5b92ce45bdfad',
content: '4 Comment',
author: 'Bob',
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: false
}],
isArchieved: false,
isParent: true
},
{
_id: 58deac3c23b5b92ce45bdfae,
topicId: '58dd6f541919c541dbf9632d',
parentId: null,
content: '3 Post',
author: 'Bob',
isAnon: false,
__v: 0,
comments: [{
_id: 58deac5e23b5b92ce45bdfaf,
topicId: '58dd6f541919c541dbf9632d',
parentId: '58deac3c23b5b92ce45bdfae',
content: '1 Comment',
author: 'Kat',
isAnon: false,
__v: 0,
comments: [],
isArchieved: false,
isParent: false
}],
isArchieved: false,
isParent: true
}
]
我知道我需要在数组中递归,但我被中间的逻辑难住了。任何帮助都将不胜感激。非常感谢。您可以使用对象收集所有节点,并将找到的子节点附加到
comments
属性。将parentId==null的节点作为根节点获取
这种方法适用于任何深度的未排序数据,并使用单个循环获得嵌套结果
var data=[{{u id:'58deac223b5b92ce45bdfac',topicId:'58dd6f541919c541dbf9632d',parentId:null,content:'1 Post',author:'Bob',createdAt:'2017-03-31T19:21:06.698Z',isAnon:false,\u v:0,注释:[],isarchived:false,isParent:true},{u id:'58deac2c23b5b92ce45bdfad',topicId:'58dd6f541919c541dbf9632d',parentId:null,content:'2 Post',author:'Bob',createdAt:'2017-03-31T19:21:16.622Z',isAnon:false,_v:0,注释:[],isarchived:false,isParent:true},{u id:'58deac3c23b5b92ce45bdfae',topicId:'58dd6f541919c541dbf9632d',parentId:null,content:'3 Post',author:'Bob',createdAt:'2017-03-31T19:21:32.682Z',isAnon:false,_v:0,注释:[],isarchived:false,isParent:true},{u id:'58deac5e23b5b92ce45bdfaf',topicId:'58dd6f541919c541dbf9632d',parentId:'58deac3c23b5b92ce45bdfae',content:'1 Comment',author:'Kat',createdAt:'2017-03-31T19:22:06.974Z',isAnon:false,_v:0,comments:[],isarchived:false,isParent:false},{u id:'58deac6c23b5b92ce45bdfb0',topicId:'58dd6f541919c541dbf9632d',parentId:'58deac2223b5b92ce45bdfac',content:'2 Comment',author:'Dave',createdAt:'2017-03-31T19:22:20.071Z',isAnon:false,_v:0,comments:[],isarchived:false,isParent:false},{id:'58deac7a23b5b92ce45bdfb1',主题id:'58dd6f541919c541dbf9632d',父id:'58deac2c23b5b92ce45bdfad',内容:'4注释',作者:'Bob',创建日期:'2017-03-31T19:22:34.865Z',isAnon:false,u v:0,注释:[],IsArcheved:false,isParent:false},
树=函数(数据,根){
var r=[],
o={};
data.forEach(函数(a){
a、 comments=(o[a.[u id]&&o[a.[u id].comments | |[]).concat();
o[a._id]=a;
if(a.parentId==根){
r、 推(a);
}否则{
o[a.parentId]=o[a.parentId]|{};
o[a.parentId].comments=o[a.parentId].comments | |[];
o[a.parentId].comments.push(a);
}
});
返回r;
}(数据,空);
console.log(树);
.as console wrapper{max height:100%!important;top:0;}
您可以使用对象收集所有节点,并将找到的子节点附加到comments
属性。将parentId==null的节点作为根节点获取
这种方法适用于任何深度的未排序数据,并使用单个循环获得嵌套结果
var data=[{{u id:'58deac223b5b92ce45bdfac',topicId:'58dd6f541919c541dbf9632d',parentId:null,content:'1 Post',author:'Bob',createdAt:'2017-03-31T19:21:06.698Z',isAnon:false,\u v:0,注释:[],isarchived:false,isParent:true},{u id:'58deac2c23b5b92ce45bdfad',topicId:'58dd6f541919c541dbf9632d',parentId:null,content:'2 Post',author:'Bob',createdAt:'2017-03-31T19:21:16.622Z',isAnon:false,_v:0,注释:[],isarchived:false,isParent:true},{u id:'58deac3c23b5b92ce45bdfae',topicId:'58dd6f541919c541dbf9632d',parentId:null,content:'3 Post',author:'Bob',createdAt:'2017-03-31T19:21:32.682Z',isAnon:false,_v:0,注释:[],isarchived:false,isParent:true},{u id:'58deac5e23b5b92ce45bdfaf',topicId:'58dd6f541919c541dbf9632d',parentId:'58deac3c23b5b92ce45bdfae',content:'1 Comment',author:'Kat',createdAt:'2017-03-31T19:22:06.974Z',isAnon:false,_v:0,comments:[],isarchived:false,isParent:false},{u id:'58deac6c23b5b92ce45bdfb0',topicId:'58dd6f541919c541dbf9632d',parentId:'58deac2223b5b92ce45bdfac',content:'2 Comment',author:'Dave',createdAt:'2017-03-31T19:22:20.071Z',isAnon:false,_v:0,comments:[],isarchived:false,isParent:false},{id:'58deac7a23b5b92ce45bdfb1',主题id:'58dd6f541919c541dbf9632d',父id:'58deac2c23b5b92ce45bdfad',内容:'4注释',作者:'Bob',创建日期:'2017-03-31T19:22:34.865Z',isAnon:false,u v:0,注释:[],IsArcheved:false,isParent:false},
树=函数(数据,根){
var r=[],
o={};
data.forEach(函数(a){
a、 comments=(o[a.[u id]&&o[a.[u id].comments | |[]).concat();
o[a._id]=a;
if(a.parentId==根){
r、 推(a);
}否则{
o[a.parentId]=o[a.parentId]|{};
o[a.parentId].comments=o[a.parentId].comments | |[];
o[a.parentId].comments.push(a);
}
});
返回r;
}(数据,空);
console.log(树);
.as控制台包装{最大高度:100%!重要;顶部:0;}
Awesome saint!谢谢,不过,我有一个问题,如果我有更深的嵌套对象怎么办?如果注释有注释等等?这仍然适用吗?你真的很棒,你知道吗?非常感谢你。嘿,尼娜,我喜欢这个函数,但我遇到了一个关于angular和traverse的问题:(.所以我想了想,然后回到这个函数。难道我不能用一个简单的计数器来限制评论深度吗?你可以添加一个计数器,如果它达到某个限制,就退出。但是这样它就不会有我所有的评论了。哈哈,该死的。