I am trying to find all the child elements of a node in the following Javascript Object.
var graphObj = {
a : {
'true' : ['e', 'i'],
'false' : ['u'],
'blah' : 'extra key'
},
e : {
'true' : ['o'],
'false' : ['v'],
'blah' : 'extra key'
},
f : {
'true' : [],
'false' : [],
'blah' : 'extra key'
},
i : {
'true' : [],
'false' : ['f'],
'blah' : 'extra key'
},
o : {
'true' : [],
'false' : [],
'blah' : 'extra key'
},
u: {
'true': [],
'false': [],
'blah' : 'extra key'
},
v: {
'true': [],
'false': [],
'blah' : 'extra key'
},
z: {
'true': [],
'false': [],
'blah' : 'extra key'
},
};
This method will return the children of a given node
var getChilds = function (opId) {
var r;
if (graphObj.hasOwnProperty(opId)) {
var t = graphObj[opId].true.slice();
var f = graphObj[opId].false.slice();
r = t.concat(f);
} else {
console.log('No node found with the ID');
}
return r;
}
console.log(getChilds('a'));
current [output] => ['e', 'i', 'u']
But I need a way to accumulate all the child nodes by recursively traversing the graph.
required output => ['e', 'i', 'u', 'o', 'v', 'f']
Can anyone help me?
Note : This is a directed graph. No loops. Also true and false are the only keys on the node storing edge types.
graphObjfor the function you want? As in, define a unit test case?