javascript - Build a JSON tree from materialized paths - Stack Overflow

I'm planning on using materialized paths in MongoDB to represent a tree and need to convert the ma

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 inprehensible.

Thanks, Neville

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 inprehensible.

Thanks, Neville

Share Improve this question edited May 23, 2017 at 10:34 CommunityBot 11 silver badge asked Jan 8, 2012 at 22:35 nevfnevf 4,8266 gold badges34 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3
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, ment){

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

            }
            var tplComment = [];

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

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

            }

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745174539a4615125.html

相关推荐

  • javascript - Build a JSON tree from materialized paths - Stack Overflow

    I'm planning on using materialized paths in MongoDB to represent a tree and need to convert the ma

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信