Build a JSON tree from materialized paths

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 05:49:28

问题


I'm planning on using materialized paths in MongoDB to represent a tree and need to convert the materialized paths back into a JSON tree.

ex. // Materialized path

var input = [
    {"id": "0", "path": "javascript" },
    {"id": "1", "path": "javascript/database" },
    {"id": "2", "path": "javascript/database/tree" },
    {"id": "3", "path": "javascript/mvc" },
    {"id": "4", "path": "javascript/mvc/knockout.js"},
    {"id": "5", "path": "javascript/mvc/backbone.js"},
    {"id": "6", "path": "c++" },
    {"id": "7", "path": "c++/c0xx"},
    {"id": "8", "path": "c++/c0xx/lambda expressions"},
    {"id": "9", "path": "c++/c0xx/vc10" }
];

The result would be:

[
    {
        "id": "0",
        "name": "javascript",
        "children": [
            {
                "id": "1",
                "name": "database",
                "children": [
                    {
                        "id": "2",
                        "name": "tree",
                        "children": []
                    }
                ]
            },
            {
                "id": "3",
                "name": "mvc",
                "children": [
                    {
                        "id": "4",
                        "name": "knockout.js",
                        "children": []
                    },
                    {
                        "id": "5",
                        "name": "backbone.js",
                        "children": []
                    }
                ]
            }
        ]
    },
    {
        "id": "6",
        "name": "c++",
        "children": [
            {
                "id": "7",
                "name": "c0xx",
                "children": [
                    {
                        "id": "8",
                        "name": "lambda expressions",
                        "children": []
                    },
                    {
                        "id": "9",
                        "name": "vc10",
                        "children": []
                    }
                ]
            }
        ]
    }
]

I found Convert delimited string into hierarchical JSON with JQuery which works fine.

And I also found Build tree from materialized path which is written in Ruby and uses recursion. I'm interested and curious to see this implemented in Javascript and wonder whether there are any folks that are fluent in both Ruby and Javascript who would like to rewrite it. I did try a Ruby to JS converter, but the result was incomprehensible.

Thanks, Neville


回答1:


var Comment = new Schema({
    date      : {
        type        : Date,
        default     : Date.now
    },
    event: ObjectId,
    body      : String,
    pathComment  : String,
    user: Array
})
Comment.virtual('level').get(function() {
    return this.pathComment.split(',').length;
});

Comment.find({event: event.id}).sort({pathComment:1}).exec(function(err, comment){

            var collectComment = function(comment){
                return  {
                    body: comment.body,
                    event: comment.event,
                    pathComment: comment.pathComment,
                    id: comment._id,
                    level: comment.level,
                    user: comment.user[0],
                    date: comment.date,
                    comments: []
                };

            }
            var tplComment = [];

            var createChildComment = function(comment, currentNode, level){

                if(level==1){
                    comment.push(collectComment(currentNode));
                }else{
                    createChildComment(comment[comment.length-1]['comments'], currentNode,level-1);
                }
                return;

            }

            for(var k in comment){
               createChildComment(tplComment, comment[k],comment[k].level);
            }
});


来源:https://stackoverflow.com/questions/8781983/build-a-json-tree-from-materialized-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!