2016-05-26 1 views
0

Ich habe Adjazenz Daten. mit IDs und Eltern-IDs,JSON hierarchisch von Adjazenzliste

Ich versuche, es in hierarchische Daten, mit verschachtelten JSON.

Ich habe es funktioniert, aber wenn ich verschiedene Objekte mit den gleichen IDs aber unterschiedliche Eltern-IDs haben. Die Daten werden durcheinander gebracht. Hier ist mein Freund. https://jsfiddle.net/Chris1807/e6s8qom3/2/

var makeTree = (function() { 
    var isArray = function(obj) { 
    return Object.prototype.toString.call(obj) == "[object Array]"; 
    }; 
    var clone = function(obj) { 
    return JSON.parse(JSON.stringify(obj)); 
    }; 
    var buildTree = function(catalog, structure, start) { 
    return (structure[start] || []).map(function(nodeid, index) { 
     var record = catalog[nodeid]; 
     var keys = structure[start][index]; 
     var children = isArray(keys) ? keys.map(function(key) { 
     return buildTree(catalog, structure, key); 
     }) : buildTree(catalog, structure, keys); 
     if (children.length) { 
     record.children = children; 
     } 
     return record; 
    }) 
    }; 
    return function(flat) { 
    var catalog = flat.reduce(function(catalog, record) { 
     catalog[record.nodeid] = clone(record); 
     delete(catalog[record.nodeid].parentid); 
     return catalog; 
    }, {}); 
    var structure = flat.reduce(function(tree, record) { 
     (tree[record.parentid] = tree[record.parentid] || []).push(record.nodeid); 
     return tree; 
    }, {}); 
    return buildTree(catalog, structure, '#'); // this might be oversimplified. 
    } 
}()); 


var flat = [{ 
    nodeid: "123802475", 
    parent: "#", 
    parentid: "#", 
    text: "CONNECTOR", 
    typepart: "Component", 
    icon: "glyphicon glyphicon-leaf", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "123802476", 
    parent: "123802475", 
    parentid: "123802475", 
    text: "PLATE", 
    typepart: "SemiComponent", 
    icon: "glyphicon glyphicon-fire", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "90615593", 
    parent: "123802476", 
    parentid: "123802476", 
    text: "C3604", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1396", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Copper", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1762", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Iron", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1061", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Lead", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3281499", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Zinc (metal)", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3402", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Tin", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "9505195", 
    parent: "123802476", 
    parentid: "123802476", 
    text: "e-plate Sn (electrodeposited Tin Coatings, bright and matt)", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1995", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Carbon", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3065", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Sulphur", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1061", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Lead", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3402", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Tin", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "749088", 
    parent: "123802476", 
    parentid: "123802476", 
    text: "e-plate Ni (bright) (electrodeposited Nickel bright)", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1995", 
    parent: "749088", 
    parentid: "749088", 
    text: "Carbon", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3112", 
    parent: "749088", 
    parentid: "749088", 
    text: "Nitrogen", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3065", 
    parent: "749088", 
    parentid: "749088", 
    text: "Sulphur", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "2619", 
    parent: "749088", 
    parentid: "749088", 
    text: "Nickel", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "123802477", 
    parent: "123802475", 
    parentid: "123802475", 
    text: "INSULATOR", 
    typepart: "SemiComponent", 
    icon: "glyphicon glyphicon-fire", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "123799238", 
    parent: "123802477", 
    parentid: "123802477", 
    text: "Polypropylene", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "238", 
    parent: "123799238", 
    parentid: "123799238", 
    text: "Polypropylene", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "9999", 
    parent: "123799238", 
    parentid: "123799238", 
    text: "Misc., not to declare", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "123802478", 
    parent: "123802475", 
    parentid: "123802475", 
    text: "LOCK COVER", 
    typepart: "SemiComponent", 
    icon: "glyphicon glyphicon-fire", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "123795153", 
    parent: "123802478", 
    parentid: "123802478", 
    text: "Pa46-GF30", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "33167", 
    parent: "123795153", 
    parentid: "123795153", 
    text: "Polytetramethylene adipic acid dimide", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "23374", 
    parent: "123795153", 
    parentid: "123795153", 
    text: "GF-Fibre", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "883", 
    parent: "123795153", 
    parentid: "123795153", 
    text: "Antimonytrioxide", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1432179", 
    parent: "123795153", 
    parentid: "123795153", 
    text: "Brominated Epoxy", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1226985", 
    parent: "123795153", 
    parentid: "123795153", 
    text: "Pigment portion, not to declare", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "123802479", 
    parent: "123802475", 
    parentid: "123802475", 
    text: "CONTACT PIN", 
    typepart: "SemiComponent", 
    icon: "glyphicon glyphicon-fire", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "90615593", 
    parent: "123802479", 
    parentid: "123802479", 
    text: "C3604", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1396", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Copper", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1762", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Iron", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1061", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Lead", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3281499", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Zinc (metal)", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3402", 
    parent: "90615593", 
    parentid: "90615593", 
    text: "Tin", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "749088", 
    parent: "123802479", 
    parentid: "123802479", 
    text: "e-plate Ni (bright) (electrodeposited Nickel bright)", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1995", 
    parent: "749088", 
    parentid: "749088", 
    text: "Carbon", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3112", 
    parent: "749088", 
    parentid: "749088", 
    text: "Nitrogen", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3065", 
    parent: "749088", 
    parentid: "749088", 
    text: "Sulphur", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "2619", 
    parent: "749088", 
    parentid: "749088", 
    text: "Nickel", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "757906", 
    parent: "123802479", 
    parentid: "123802479", 
    text: "e-plate Au (functional) (electrodeposited functional Gold Coatings)", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1995", 
    parent: "757906", 
    parentid: "757906", 
    text: "Carbon", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3112", 
    parent: "757906", 
    parentid: "757906", 
    text: "Nitrogen", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "10153", 
    parent: "757906", 
    parentid: "757906", 
    text: "Gold", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "123802480", 
    parent: "123802475", 
    parentid: "123802475", 
    text: "BODY", 
    typepart: "SemiComponent", 
    icon: "glyphicon glyphicon-fire", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "73794193", 
    parent: "123802480", 
    parentid: "123802480", 
    text: "C5191", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1396", 
    parent: "73794193", 
    parentid: "73794193", 
    text: "Copper", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1762", 
    parent: "73794193", 
    parentid: "73794193", 
    text: "Iron", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1061", 
    parent: "73794193", 
    parentid: "73794193", 
    text: "Lead", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3281499", 
    parent: "73794193", 
    parentid: "73794193", 
    text: "Zinc (metal)", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3402", 
    parent: "73794193", 
    parentid: "73794193", 
    text: "Tin", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "2835", 
    parent: "73794193", 
    parentid: "73794193", 
    text: "Phosphorus", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "9999", 
    parent: "73794193", 
    parentid: "73794193", 
    text: "Misc., not to declare", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "9505195", 
    parent: "123802480", 
    parentid: "123802480", 
    text: "e-plate Sn (electrodeposited Tin Coatings, bright and matt)", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1995", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Carbon", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3065", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Sulphur", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1061", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Lead", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3402", 
    parent: "9505195", 
    parentid: "9505195", 
    text: "Tin", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "749088", 
    parent: "123802480", 
    parentid: "123802480", 
    text: "e-plate Ni (bright) (electrodeposited Nickel bright)", 
    typepart: "Material", 
    icon: "glyphicon glyphicon-grain", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "1995", 
    parent: "749088", 
    parentid: "749088", 
    text: "Carbon", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3112", 
    parent: "749088", 
    parentid: "749088", 
    text: "Nitrogen", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "3065", 
    parent: "749088", 
    parentid: "749088", 
    text: "Sulphur", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}, { 
    nodeid: "2619", 
    parent: "749088", 
    parentid: "749088", 
    text: "Nickel", 
    typepart: "Substance", 
    icon: "glyphicon glyphicon-asterisk", 
    Count: 0, 
    state: null 
}]; 
+0

Wenn 'nodeid' nicht eindeutig ist, dann was werden Sie tun, wenn Sie ein "parentid" bezieht sich auf einen solchen nicht eindeutigen Wert? – trincot

+0

@trincot Die Eltern-ID ist eindeutig. Eine NodeID mit einer Eltern-ID sollte an diese bestimmte Eltern-ID angehängt werden. – ChrisPasa

Antwort

1

Die Lösung ist wirklich eindeutigen Schlüssel im catalog Objekt zu verwenden, um zu verhindern, dass Sie zuvor gespeicherte Objekte zu überschreiben. Wie Sie geschrieben haben, dass nodeid Werte garantiert einzigartig sind, wenn sie Kinder haben, können Sie einfach nodeid mit dem parentid voranstellen, um Ihre catalog Schlüssel einzigartig zu machen.

also nur drei Zeilen ändern müssen:

var record = catalog[start + '/' + nodeid]; 

und:

catalog[record.parentid + '/' + record.nodeid] = clone(record); 
    delete(catalog[record.parentid + '/' + record.nodeid].parentid); 

Das ist es.

Als Performance-Verbesserung würde ich nicht verwenden JSON-Text-Konvertierung vorschlagen Objekte zu kopieren, aber Object.assign stattdessen zu verwenden:

var clone = function(obj) { 
    return Object.assign({}, obj); 
}; 
+0

danke, aber jetzt habe ich Duplikate auf den letzten Ebenen. Referenziere mein aktuelles Jfiddle. – ChrisPasa

+0

Können Sie bitte die ID geben, wo die Dinge falsch sind, so muss ich nicht suchen? – trincot

+0

"nodeid": "1396", – ChrisPasa

Verwandte Themen