Ich habe die Aufgabe, eine Stammbaumwebseite mit D3.JS und DTREE zu entwickeln. Im Front-End übernimmt JavaScript JSON-Daten, die die ID der Beziehungstyp enthalten. (Ex: 1 für den Großvater für Eltern, 2 für S. Großmutter usw.) < /p>
Ich habe Probleme, neue Knoten hinzuzufügen. Die aktuelle Logik prüft RelationType im Switch und findet den Knoten mit Ausdehnung Kette an: < /p>
function buildTreeHierarchy(relations) {
const nodeMap = new Map();
const treeData = [
];
//relationType relationName
//1 PGrandfather
//2 PGrandmother
//3 PUncle
//4 PAunt
//5 Father
//6 MGrandfather
//7 MGrandmother
//8 MUncle
//9 MAunt
//10 Mother
//11 Spouse
//12 Child
//13 Sibling
// "relations" list is sorted ascending order by relationtype
for (const rel of relations) {
const newNode = {
name: rel.toName,
class: rel.toIsLiving ? "alive" : "dead",
photo: rel.toPhoto,
};
switch (rel.relationType) {
case 1: { // adds parental grandfather
newNode.marriages = [];
treeData.push(newNode);
break;
}
case 2: { // adds parental grandmother
if (treeData[0].name === 'Grandfather') {
treeData[0].marriages[0].push({
spouse: newNode,
});
}
else { // if grandfather does not exits, add grandmother directly
treeData.push(newNode);
}
break;
}
case 3: { // uncle
// the problem is here, in further cases it will be very long
treeData[0].marriages[0].children.push(newNode);
break;
}
// other relationships
}
}
return treeData;
}
< /code>
Dies macht den Code nicht nachhaltig und schwer zu verstehen. Wie kann ich eine richtige Logik bauen?
Stammbaumalgorithmus [geschlossen] ⇐ JavaScript
-
- Similar Topics
- Replies
- Views
- Last post