I've been trying to find the solution for a few days but no success. It is only exemple I need of a function that execute this with any selectedNode. I will use it in a graph.
The array:
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"
}
]
I trying use filter and map but only catch the first children. I need the children of children and so on.
const selectedNode = "2";
const connections = edges
.filter((edge) => edge.from === selectedNode).map((edge) => edge.to)
console.log(connections)
The output:
[LOG]: ["3", "8", "13"]
but i need all children as cascade.
- Like this output:
["3", "8", "13", "4", "5", "6", "7", "9", "10", "11", "12", "14", "15", "16", "17", "18", "19"]
- But if the selectedNode was "8", the array would be:
["9", "10", "11", "12"]
CodePudding user response:
You need to make a recursive algorithm of some kind. One approach would be to restructure the edges into a mapping by from
s first. Then, when iterating over an edge (starting with the selectedNode
), if the edge's to
contains any other to
s, perform the recursive call for each one.
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'));
CodePudding user response:
This sounds like a recursion mission. This is the same answer as the other one. Only as a function.
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));