简体   繁体   English

如何找到链的最后一个孩子

[英]How to find last child of chain

How to find the last element of this chain given in the code is the array(Symptomaster history array). 如何在代码中找到此链中的最后一个元素是数组(Symptomaster历史数组)。

I have used recursion. 我用过递归。 I will show in the code. 我将在代码中显示。

//example data in array
0: SymptomasterHistory {parentId: -1, currentId: 798, rootBool: true, hasChild: true}
1: SymptomasterHistory {parentId: -1, currentId: 235, rootBool: true, hasChild: true}
2: SymptomasterHistory {parentId: 798, currentId: 799, rootBool: false, hasChild: false}
3: SymptomasterHistory {parentId: 235, currentId: 237, rootBool: false, hasChild: true}
4: SymptomasterHistory {parentId: 237, currentId: 274, rootBool: false, hasChild: false}

// recursion
findResultByParentId(parentId: number) {
        for (let i = 0; i < this.childSymptomaster.length; i++) {
            if(this.childSymptomaster[i].parentId === parentId && this.childSymptomaster[i].hasChild){
                this.findResultByParentId(this.childSymptomaster[i].currentId);
            }else{
                let id = this.childSymptomaster[i].currentId;
                this.childSymptomaster.splice(i,1);
                return id;
            }
        }
    }

Example of using recursion, parent id is 798 and the result should be 799. Help me, please. 使用递归的示例,父ID是798,结果应该是799.请帮帮我。

The reason it doesn't work for you is because you have the recursive case inside the loop. 它不适合你的原因是因为你在循环中有递归的情况。 In reality you should do this: 实际上你应该这样做:

function findResultByParentId(id) {
        // Find the current element
        var currentElement = this.childSymptomaster.find(function(e) { 
          return e.currentId === id;
        });
        // If current element id doesn't exist
        if (!currentElement) {
          // Return error value -1, null, undefined or similar
          return -1;
        }

        if (currentElement.hasChild) {
          // Recursive case
          // The element have a child - find it...
          var child = this.childSymptomaster.find(function(c) {
            return c.parentId === id;
          });
          // check for that child
          return findResultByParentId(child.currentId);
        } else {
          // Base case - we are at the bottom leaf
          return currentElement.currentId;
        }
    }

For a more compressed version using arrow functions: 对于使用箭头功能的更加压缩的版本:

function findResultByParentId(id) {
        // Find the current element
        let currentElement = this.childSymptomaster.find((e) => e.currentId === id);
        // If current element id doesn't exist
        if (!currentElement) {
          // Return error value -1, null, undefined or similar
          return -1;
        }

        if (currentElement.hasChild) {
          // Recursive case
          // The element have a child - find it...
          let child = this.childSymptomaster.find((c) => c.parentId === id);
          // Check for that child
          return findResultByParentId(child.currentId);
        } else {
          // Base case - we are at the bottom leaf
          return currentElement.currentId;
        }
    }

For a working plunker see this: https://plnkr.co/edit/leSUKQCpt5xCnFvSR0ca?p=preview 对于一个工作的plunker看到这个: https ://plnkr.co/edit/leSUKQCpt5xCnFvSR0ca?p = preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM