递归搜索时,必须通过返回结果将其传回。不过,您没有返回的结果
findNode(id, currentChild)。
function findNode(id, currentNode) { var i, currentChild, result; if (id == currentNode.id) { return currentNode; } else { // Use a for loop instead of forEach to avoid nested functions // Otherwise "return" will not work properly for (i = 0; i < currentNode.children.length; i += 1) { currentChild = currentNode.children[i]; // Search in the current child result = findNode(id, currentChild); // Return the result if the node has been found if (result !== false) { return result; } } // The node has not been found and we have no more options return false; }}


