admin管理员组文章数量:1291127
I build a simple Javascript treeview, where every Node has a name, a list of children and an ID:
class Node {
constructor(name, childNodes, id) {
this.name = name;
this.childNodes = childNodes;
this.id = id;
}
}
My Aim is to create a function "getNodeById(parent, id)", that returns the Node with the given ID. I have tried using a recursive method, but there has to be a mistake somewhere:
function getNodeById(currentNode, id) {
if (currentNode.id === id) { console.log("found"); return currentNode; }
currentNode.children.forEach(child => {
console.log(child);
return getNodeById(child, id);
});
}
My idea is, that the function will search through the childNodes and call itsself again. When the the correct node is found, it should be returned after the if-statement, then inside the foreach loop. The function finds the right node successfully but it won't stop and return the Node. Here is the output when calling getNodeById(parent, 2);
Why doesn't the function exit after the "found"?
I build a simple Javascript treeview, where every Node has a name, a list of children and an ID:
class Node {
constructor(name, childNodes, id) {
this.name = name;
this.childNodes = childNodes;
this.id = id;
}
}
My Aim is to create a function "getNodeById(parent, id)", that returns the Node with the given ID. I have tried using a recursive method, but there has to be a mistake somewhere:
function getNodeById(currentNode, id) {
if (currentNode.id === id) { console.log("found"); return currentNode; }
currentNode.children.forEach(child => {
console.log(child);
return getNodeById(child, id);
});
}
My idea is, that the function will search through the childNodes and call itsself again. When the the correct node is found, it should be returned after the if-statement, then inside the foreach loop. The function finds the right node successfully but it won't stop and return the Node. Here is the output when calling getNodeById(parent, 2);
Why doesn't the function exit after the "found"?
Share Improve this question asked Apr 25, 2018 at 15:03 BastiBasti 5174 silver badges21 bronze badges 04 Answers
Reset to default 4function getNodeById(currentNode, id) {
if (currentNode.id === id) { console.log("found"); return currentNode; }
for(node = 0; node < currentNode.children.length; node ++) {
const foundNode = getNodeById(currentNode.children[node], id);
if(foundNode) {
return foundNode
}
}
return undefined
}
This should work!!
You could use a short circuit with Array#some
and return a stored node, if found.
some()
executes thecallback
function once for each element present in the array until it finds one wherecallback
returns a truthy value (a value that beestrue
when converted to a Boolean).
function getNodeById(currentNode, id) {
var node;
if (currentNode.id === id) {
return currentNode;
}
currentNode.children.some(child => node = getNodeById(child, id));
return node;
}
There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.
Early termination may be acplished with:
A simple loop
A for...of loop
Array.prototype.every()
Array.prototype.some()
Array.prototype.find()
Array.prototype.findIndex() The other Array methods: every(), some(), find(), and findIndex() test the array elements with a predicate returning a truthy value to determine if further iteration is required.
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
getNodeById()
doesn't return anything. You are returning from the right arrow function which the forEach()
take and doesn't doing anything with. Use a for loop.
本文标签: Javascript recursionforeach loop won39t exitStack Overflow
版权声明:本文标题:Javascript recursion, foreach loop won't exit? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524860a2383406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论