递归重构对象javascript

时间:2017-03-31 19:47:58

标签: 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({ 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 } ]

我遇到了问题,因为我需要在数据库出来之后修改这个列表以获取带有parentIds的帖子并将它们推送到相应的父级' 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
                }
            ]

我知道我需要通过阵列进行递归,但是我很难理解它们之间的逻辑。任何帮助,将不胜感激。非常感谢你。

1 个答案:

答案 0 :(得分:0)

您可以使用对象收集所有节点,并将找到的子节点附加到comments属性。获取parentId === null作为根节点的节点。

此方法适用于任何深度的未排序数据,并使用单个循环来获取嵌套结果。



var data = [{ _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 }],
    tree = function (data, root) {
        var r = [],
            o = {};

        data.forEach(function (a) {
            a.comments = (o[a._id] && o[a._id].comments || []).concat();
            o[a._id] = a;
            if (a.parentId === root) {
                r.push(a);
            } else {
                o[a.parentId] = o[a.parentId] || {};
                o[a.parentId].comments = o[a.parentId].comments || [];
                o[a.parentId].comments.push(a);
            }
        });
        return r;
    }(data, null);
    
console.log(tree);

.as-console-wrapper { max-height: 100% !important; top: 0; }