I have a function I'm using to recurse through a series of nested, tree node-like objects. Based on the console output, I can tell that all nodes are currently being visited. Here's the function:
function directoryRecurse(dir, searchedPath) {
for (var ii = 0; ii < dir.children.length; ii++) {
if (dir.children[ii].path && dir.children[ii].path === searchedPath) {
return dir.children[ii];
} else {
directoryRecurse(dir.children[ii], searchedPath);
}
}
}
However, the return value is always undefined
. I've tried modifying the function so that directoryRecurse(dir.children[ii], searchedPath)
is replaced with return directoryRecurse(dir.children[ii], searchedPath)
, but in this case the function terminates after the first leaf node is found. How do I ensure all nodes are visited and that the final return value is the node being searched for?