2012-01-08 19 views
10

Sto pianificando di utilizzare i percorsi materializzati in MongoDB per rappresentare un albero e ho bisogno di convertire i percorsi materializzati in un albero JSON.Costruire un albero JSON da percorsi materializzati

ex. // percorso

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

verificato il risultato sarebbe:

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

ho trovato Convert delimited string into hierarchical JSON with JQuery che funziona bene.

E ho anche trovato Build tree from materialized path che è scritto in Ruby e utilizza la ricorsione. Sono interessato e curioso di vederlo implementato in Javascript e mi chiedo se ci sono persone che parlano correntemente sia in Ruby che in Javascript che vorrebbero riscriverlo. Ho provato un convertitore da Ruby a JS, ma il risultato è stato incomprensibile.

Grazie, Neville

risposta

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, 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); 
      } 
}); 
+1

Grazie. Ho finito per usare il codice da http://stackoverflow.com/questions/6232753/convert-delimited-string-into-hierarchical-json-with-jquery che funziona bene per le mie esigenze. Alcuni commenti su come funziona il codice sarebbero i benvenuti. – nevf

Problemi correlati