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 0
Add a ment  | 

4 Answers 4

Reset to default 4
function 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 the callback function once for each element present in the array until it finds one where callback returns a truthy value (a value that bees true 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