Мне поручено разработать веб -страницу семейного дерева с использованием D3.js и Dtree. На переднем конце JavaScript обрабатывает данные JSON, которые включают идентификатор типа отношения. (Пример: 1 для родительской дедушки, 2 для стр. Бабушка и т. Д.) < /p>
У меня проблемы с добавлением новых узлов. Текущая логика проверяет отношение к переключению и добавляет узел с расширяющейся цепью: < /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>
Это делает код неустойчивым и трудным для понимания. Как построить правильную логику?
Подробнее здесь: https://stackoverflow.com/questions/797 ... -algorithm
Алгоритм семейного дерева [закрыто] ⇐ Javascript
Форум по Javascript
1755509564
Anonymous
Мне поручено разработать веб -страницу семейного дерева с использованием D3.js и Dtree. На переднем конце JavaScript обрабатывает данные JSON, которые включают идентификатор типа отношения. (Пример: 1 для родительской дедушки, 2 для стр. Бабушка и т. Д.) < /p>
У меня проблемы с добавлением новых узлов. Текущая логика проверяет отношение к переключению и добавляет узел с расширяющейся цепью: < /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>
Это делает код неустойчивым и трудным для понимания. Как построить правильную логику?
Подробнее здесь: [url]https://stackoverflow.com/questions/79738442/family-tree-algorithm[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия