So I have this flat array which has three arrays:
[
[
"a198f9db5e814b11b6e8b885b7978250",
"New Section",
0,
0,
0,
0
],
[
"F0CA2865AA708377EE73A64F98B04BE0",
"New Section",
0,
0,
2,
1
],
[
"B3BADFC6F5A5ECAC1619A651A33C2692",
"New Section",
0,
0,
14,
2
]
]
数组中的第 5 个元素是我的部分深度,所以我想做的是在 javascript 中构建一个树结构。但我不知道也不明白如何做到这一点。就代码而言,这是我现在所处的位置:
var input = flatArray;
var root = {nodes:[], depth: -1};
var ancester_branch = [root];
var current_target = root;
for(var i = 0; i < input.length; i++){
var node = input[i];
while(node.depth <= current_target.depth){
ancester_branch.pop();
current_target = ancester_branch[-1];
}
current_target.nodes.push(node);
current_target = node;
ancester_branch.push(node);
return root;
};
my var input = rToc.metadata[0].sections;
var root = {nodes:[], depth: -1};
var ancester_branch = [root];
var current_target = root;
for(var i = 0; i < input.length; i++){
var node = input[i];
while(node.depth <= current_target.depth){
ancester_branch.pop();
current_target = ancester_branch[-1];
}
current_target.nodes.push(node);
current_target = node;
ancester_branch.push(node);
return root;
};
var input = rToc.metadata[0].sections;
var root = {nodes:[], depth: -1};
var ancester_branch = [root];
var current_target = root;
for(var i = 0; i < input.length; i++){
var node = input[i];
while(node.depth <= current_target.depth){
ancester_branch.pop();
current_target = ancester_branch[-1];
}
current_target.nodes.push(node);
current_target = node;
ancester_branch.push(node);
return root;
};
var input = rToc.metadata[0].sections;
var root = {nodes:[], depth: -1};
var ancester_branch = [root];
var current_target = root;
for(var i = 0; i < input.length; i++){
var node = input[i];
while(node.depth <= current_target.depth){
ancester_branch.pop();
current_target = ancester_branch[-1];
}
current_target.nodes.push(node);
current_target = node;
ancester_branch.push(node);
return root;
};
var input = rToc.metadata[0].sections;
var root = {nodes:[], depth: -1};
var ancester_branch = [root];
var current_target = root;
for(var i = 0; i < input.length; i++){
var node = input[i];
while(node.depth <= current_target.depth){
ancester_branch.pop();
current_target = ancester_branch[-1];
}
current_target.nodes.push(node);
current_target = node;
ancester_branch.push(node);
return root;
};
我的 ancester_branch 是在我构建树并从中推送和弹出时跟踪节点。我不知道从这里去哪里或做什么。所以我希望有人可以帮助我提前感谢您的帮助。