chainList transform to renderTree(Array)

Posted on 2022-05-16 22:42:01
Author: 可乐小可爱メ

1. renderTree to chain

const renderTree = [
{
name: "A",
parent: "step1",
child: "Aa",
siblings: "B",
children: [
{
name: "Aa",
parent: "A",
child: "Aa1",
siblings: "Ab",
children: [
{ name: "Aa1", parent: "Aa", child: null, siblings: "Ab1" },
{
name: "Ab1",
parent: "Aa",
child: "Ab1-1",
siblings: "Ac1",
children: [
{
name: "Ab1-1",
parent: "Ab1",
child: null,
siblings: "Ab1-2",
},
{
name: "Ab1-2",
parent: "Ab1",
child: "Ab1-2-1",
siblings: "Ab1-3",
children: [
{
name: "Ab1-2-1",
parent: "Ab1-2",
child: null,
siblings: null,
},
],
},
{ name: "Ab1-3", parent: "Ab1", child: null, siblings: null },
],
},
{
name: "Ac1",
parent: "Aa",
child: "Ac1-1",
siblings: null,
children: [
{
name: "Ac1-1",
parent: "Ac1",
child: null,
siblings: "Ac1-2",
},
{
name: "Ac1-2",
parent: "Ac1",
child: "Ac1-2-1",
siblings: null,
children: [
{
name: "Ac1-2-1",
parent: "Ac1-2",
child: null,
siblings: null,
},
],
},
],
},
],
},
{ name: "Ab", parent: "A", child: null, siblings: "Ac" },
{ name: "Ac", parent: "A", child: null, siblings: "Ad" },
{
name: "Ad",
parent: "A",
child: "Ad1",
siblings: null,
children: [
{ name: "Ad1", parent: "Ad", child: null, siblings: "Ad2" },
{ name: "Ad2", parent: "Ad", child: null, siblings: null },
],
},
],
},
{
name: "B",
parent: "step1",
child: "B1",
siblings: "C",
children: [{ name: "B1", parent: "B", child: null, siblings: null }],
},
{ name: "C", parent: "step1", child: null, siblings: null },
];

// 简写
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}

const obj = {};
function arrayToChain(arr) {
for (let i = 0; i < arr.length; i++) {
const elm = deepClone(arr[i]);
delete elm.children;
obj[elm.name] = elm;
if (elm.children && elm.children.length > 0) {
arrayToChain(elm.children);
}
}
}
arrayToChain(renderTree);
console.log("obj:: ", obj);



2. chain to renderTree

const chainedList = {
A: {
name: "A",
parent: "step1",
child: "Aa",
siblings: "B",
},
B: {
name: "B",
parent: "step1",
child: "B1",
siblings: "C",
},
C: {
name: "C",
parent: "step1",
child: null,
siblings: null,
},
B1: {
name: "B1",
parent: "B",
child: null,
siblings: null,
},
Aa: {
name: "Aa",
parent: "A",
child: "Aa1",
siblings: "Ab",
},
Aa1: {
name: "Aa1",
parent: "Aa",
child: null,
siblings: "Ab1",
},
Ab1: {
name: "Ab1",
parent: "Aa",
child: "Ab1-1",
siblings: "Ac1",
},
"Ab1-1": {
name: "Ab1-1",
parent: "Ab1",
child: null,
siblings: "Ab1-2",
},
"Ab1-2": {
name: "Ab1-2",
parent: "Ab1",
child: "Ab1-2-1",
siblings: "Ab1-3",
},
"Ab1-3": {
name: "Ab1-3",
parent: "Ab1",
child: null,
siblings: null,
},
"Ab1-2-1": {
name: "Ab1-2-1",
parent: "Ab1-2",
child: null,
siblings: null,
},
Ac1: {
name: "Ac1",
parent: "Aa",
child: "Ac1-1",
siblings: null,
},
Ab: {
name: "Ab",
parent: "A",
child: null,
siblings: "Ac",
},
Ac: {
name: "Ac",
parent: "A",
child: null,
siblings: "Ad",
},
Ad: {
name: "Ad",
parent: "A",
child: "Ad1",
siblings: null,
},
Ad1: {
name: "Ad1",
parent: "Ad",
child: null,
siblings: "Ad2",
},
Ad2: {
name: "Ad2",
parent: "Ad",
child: null,
siblings: null,
},
"Ac1-1": {
name: "Ac1-1",
parent: "Ac1",
child: null,
siblings: "Ac1-2",
},
"Ac1-2": {
name: "Ac1-2",
parent: "Ac1",
child: "Ac1-2-1",
siblings: null,
},
"Ac1-2-1": {
name: "Ac1-2-1",
parent: "Ac1-2",
child: null,
siblings: null,
},
};
var chainedListArray = [];
for (const key in chainedList) {
chainedListArray.push(chainedList[key]);
}
const treeList = [];
let parent = null;
let siblingsKey = "";

const uncleKeys = {};

function chainToArrayTree(currentKey) {
if (currentKey) {
const current = chainedList[currentKey];
if (uncleKeys[currentKey]) uncleKeys[currentKey] = false;
console.log("parent:: ", parent);
if (!parent) {
treeList.push(current);
} else {
parent.children = Array.isArray(parent.children)
? [...parent.children, current]
: [current];
}
// have child
if (current.child) {
parent = current;
currentKey = current.child;
siblingsKey = chainedList[currentKey].siblings;
if (current.siblings) {
uncleKeys[current.siblings] = true;
}
chainToArrayTree(currentKey);
}
// no child but have siblings
else if (!current.child && current.siblings) {
parent = chainedList[current.parent];
currentKey = siblingsKey;
siblingsKey = chainedList[currentKey]?.siblings;
chainToArrayTree(currentKey);
}

// no child and bo siblings
else if (!current.child && !current.siblings) {
// if parent have siblings
if (chainedList[current.parent]?.siblings) {
currentKey = chainedList[current.parent].siblings;
parent = chainedList[chainedList[currentKey].parent];
siblingsKey = chainedList[currentKey].siblings;
chainToArrayTree(currentKey);
} else {
// if parent no siblings, current tree end, recycle to startKey"s siblings
console.log("currentLL::", current);
currentKey = findAncestorSiblings(current);
if (currentKey) {
parent = chainedList[chainedList[currentKey].parent];
siblingsKey = chainedList[currentKey].siblings || null;
chainToArrayTree(currentKey);
}
}
}
}
}

function findAncestorSiblings(current) {
const key = current?.parent || null;
if (key) {
const siblings = chainedList[key]?.siblings || null;
if (siblings && uncleKeys[siblings]) {
return siblings;
} else {
return findAncestorSiblings(chainedList[key]);
}
}
}

chainToArrayTree("A");
console.log("treeList: ", treeList);


当前评论 (0) 登录后评论

暂无评论