2016-04-23 17 views
2

Ich habe zur Zeit die folgenden Daten in meiner DatenbankGebäude Multi-Level-Menü NodeJS

enter image description here

Die Mongo Datenbank speichert, wie dies

id parent 
1 0 
2 0 
3 1 
4 1 
5 2 
6 2 
7 2 
30 3 
31 3 
70 7 
71 7 

Jetzt mag ich die Ausgabe in einem einzigen JavaScript-Array like so mit nodejs

[ 
{id:1,sub:[ 
    {id:3, sub:[{id:30},{id:31}]}, 
    {id:4,sub:[]} 
] 
}, 
{id:2,sub:[ 
    {id:5,sub: []}, 
    {id:6,sub: []}, 
    {id:7,sub: [{id:70}, {id:71}]} 
    ] 
} 
] 

Der Zweck Dies ist im Wesentlichen die Ausgabe der Kategorie in einem Megamenu.

Antwort

0

Das folgende Beispiel zeigt eine Möglichkeit, was Sie wollen.

// Example data from the question 
 
var nodes = [ 
 
    { id: 1, parent: 0 }, 
 
    { id: 2, parent: 0 }, 
 
    { id: 3, parent: 1 }, 
 
    { id: 4, parent: 1 }, 
 
    { id: 5, parent: 2 }, 
 
    { id: 6, parent: 2 }, 
 
    { id: 7, parent: 2 }, 
 
    { id: 30, parent: 3 }, 
 
    { id: 31, parent: 3 }, 
 
    { id: 70, parent: 7 }, 
 
    { id: 71, parent: 7 } 
 
]; 
 

 
// We construct `t`, the array of parents, so that `t[i] === x` means that `x` 
 
// is the parent of `i` 
 
var t = []; 
 
for (var i = 0; i < nodes.length; i++) { 
 
    t[nodes[i].id] = nodes[i].parent; 
 
} 
 

 
// `t` represents the array of parents 
 
// `c` represents the parent whose children should be put in the outputted array 
 
function f(t, c) { 
 
    // The output structure 
 
    var a = []; 
 

 
    // We loop through all the nodes to fill `a` 
 
    for (var i = 0; i < t.length; i++) { 
 
     // If the node has the parent `c` 
 
     if (t[i] === c) { 
 
      // Create an object with the `id` and `sub` properties and push it 
 
      // to the `a` array 
 
      a.push({ 
 
       id: i, 
 
       // The `sub` property's value is generated recursively 
 
       sub: f(t, i) 
 
      }); 
 
     } 
 
    } 
 

 
    // Finish by returning the `a` array 
 
    return a; 
 
} 
 

 
// Print the outputted array in a pretty way 
 
// We call the `f` function with the 0 parameter because 0 is the parent of the 
 
// nodes that should be directly put in the returned array 
 
alert(JSON.stringify(f(t, 0)));

Auf Node.js 0.12.13 diesen Code anstelle des alert am Ende des obigen Snippet ausgeführt wird:

var util = require('util'); 
console.log(util.inspect(f(t, 0), { 
    colors: true, 
    depth: null 
})); 

druckt die folgenden:

[ { id: 1, 
    sub: 
    [ { id: 3, sub: [ { id: 30, sub: [] }, { id: 31, sub: [] } ] }, 
     { id: 4, sub: [] } ] }, 
    { id: 2, 
    sub: 
    [ { id: 5, sub: [] }, 
     { id: 6, sub: [] }, 
     { id: 7, sub: [ { id: 70, sub: [] }, { id: 71, sub: [] } ] } ] } ] 

was ich denke was du willst.

Ich lese auch this page wo eine andere Lösung, möglicherweise effizienter, beschrieben wird.

+0

Wie wäre es mit [http://stackoverflow.com/questions/38423769/how-to-create-menu-multi-levelusing-nodejs](http://stackoverflow.com/questions/38423769/how-to -create-menu-multilevel-using-nodejs) – Loint