几天来,我一直试图找到解决办法,但没有成功。仅举个例子,我需要一个用任何selectedNode来执行此操作的函数。我会用在图表上。
数组:
const edges = [
{
"id": "e1-2",
"from": "1",
"to": "2"
},
{
"id": "e2-3",
"from": "2",
"to": "3"
},
{
"id": "e2-8",
"from": "2",
"to": "8"
},
{
"id": "e2-13",
"from": "2",
"to": "13"
},
{
"id": "e3-4",
"from": "3",
"to": "4"
},
{
"id": "e4-5",
"from": "4",
"to": "5"
},
{
"id": "e4-6",
"from": "4",
"to": "6"
},
{
"id": "e4-7",
"from": "4",
"to": "7"
},
{
"id": "e8-9",
"from": "8",
"to": "9"
},
{
"id": "e9-10",
"from": "9",
"to": "10"
},
{
"id": "e9-11",
"from": "9",
"to": "11"
},
{
"id": "e9-12",
"from": "9",
"to": "12"
},
{
"id": "e13-14",
"from": "13",
"to": "14"
},
{
"id": "e14-15",
"from": "14",
"to": "15"
},
{
"id": "e14-16",
"from": "14",
"to": "16"
},
{
"id": "e14-17",
"from": "14",
"to": "17"
},
{
"id": "e14-18",
"from": "14",
"to": "18"
},
{
"id": "e14-19",
"from": "14",
"to": "19"
}
]我试着使用过滤器和地图,但只捕捉第一个孩子。我需要孩子的孩子等等。
const selectedNode = "2";
const connections = edges
.filter((edge) => edge.from === selectedNode).map((edge) => edge.to)
console.log(connections)产出:
日志:"3“、"8”、"13“
但我需要所有的孩子。
像这样的
"3“、"8”、"13“、"4”、"5“、"6”、"7“、"9”、"10“、"11”、"12“、"14”、"15“、"16”、"17“、"18”、"19“
"9“"10”"11“"12"
发布于 2022-08-01 23:23:04
您需要制定某种递归算法。一种方法是通过from的第一步将边缘重构为映射。然后,当迭代边缘(从selectedNode开始)时,如果边缘的to包含任何其他to,则对每个to执行递归调用。
const edges=[{id:"e1-2",from:"1",to:"2"},{id:"e2-3",from:"2",to:"3"},{id:"e2-8",from:"2",to:"8"},{id:"e2-13",from:"2",to:"13"},{id:"e3-4",from:"3",to:"4"},{id:"e4-5",from:"4",to:"5"},{id:"e4-6",from:"4",to:"6"},{id:"e4-7",from:"4",to:"7"},{id:"e8-9",from:"8",to:"9"},{id:"e9-10",from:"9",to:"10"},{id:"e9-11",from:"9",to:"11"},{id:"e9-12",from:"9",to:"12"},{id:"e13-14",from:"13",to:"14"},{id:"e14-15",from:"14",to:"15"},{id:"e14-16",from:"14",to:"16"},{id:"e14-17",from:"14",to:"17"},{id:"e14-18",from:"14",to:"18"},{id:"e14-19",from:"14",to:"19"}];
const toByFrom = {};
for (const edge of edges) {
toByFrom[edge.from] ??= [];
toByFrom[edge.from].push(edge.to);
}
const allNodes = [];
const getNodes = (parent, allNodes = []) => {
const tos = toByFrom[parent];
if (tos) {
allNodes.push(...tos);
for (const to of tos) {
getNodes(to, allNodes);
}
}
return allNodes;
};
console.log(getNodes('2'));
发布于 2022-08-01 23:41:53
这听起来像是递归任务。这和另一个答案是一样的。只是作为一种功能。
const edges = [{ "id": "e1-2", "from": "1", "to": "2" }, { "id": "e2-3", "from": "2", "to": "3" }, { "id": "e2-8", "from": "2", "to": "8" }, { "id": "e2-13", "from": "2", "to": "13" }, { "id": "e3-4", "from": "3", "to": "4" }, { "id": "e4-5", "from": "4", "to": "5" }, { "id": "e4-6", "from": "4", "to": "6" }, { "id": "e4-7", "from": "4", "to": "7" }, { "id": "e8-9", "from": "8", "to": "9" }, { "id": "e9-10", "from": "9", "to": "10" }, { "id": "e9-11", "from": "9", "to": "11" }, { "id": "e9-12", "from": "9", "to": "12" }, { "id": "e13-14", "from": "13", "to": "14" }, { "id": "e14-15", "from": "14", "to": "15" }, { "id": "e14-16", "from": "14", "to": "16" }, { "id": "e14-17", "from": "14", "to": "17" }, { "id": "e14-18", "from": "14", "to": "18" }, { "id": "e14-19", "from": "14", "to": "19" }];
function find_immediate_edges(selected) {
return edges.filter(item => item.from == selected).map(item => item.to);
}
function find_edges(selected) {
var result = []
function loop(selected) {
var immediate = find_immediate_edges(selected);
result = result.concat(immediate)
immediate.forEach(loop)
}
loop(selected);
return result;
}
console.log("" + find_edges(2));
console.log("" + find_edges(8));
https://stackoverflow.com/questions/73200434
复制相似问题