【问题标题】:Build a JSON tree from materialized paths从物化路径构建 JSON 树
【发布时间】:2012-01-08 22:35:46
【问题描述】:

我打算在 MongoDB 中使用物化路径来表示一棵树,并且需要将物化路径转换回 JSON 树。

例如。 // 实体化路径

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" }
];

结果是:

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

我发现Convert delimited string into hierarchical JSON with JQuery 工作正常。

我还发现了Build tree from materialized path,它是用 Ruby 编写并使用递归的。我很感兴趣和好奇地看到它在 Javascript 中实现,并想知道是否有任何人会流利地使用 Ruby 和 Javascript 来重写它。我确实尝试了一个 Ruby 到 JS 的转换器,但结果令人费解。

谢谢, 内维尔

【问题讨论】:

    标签: javascript json tree materialized-path-pattern


    【解决方案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);
                }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2017-02-05
    相关资源
    最近更新 更多