How do I write this recursive function to find the max depth of my object?

前端 未结 6 1695
庸人自扰
庸人自扰 2020-12-06 14:30

I\'m trying to write a function that will loop through my object and return the level depth of the object.

For example, if I ran the function on this object:

6条回答
  •  既然无缘
    2020-12-06 15:06

    In the function

    function getMaxDepth(root) {
        var currentMaxDepth = 0;
    
        return getDepth(currentMaxDepth, root);
    }
    

    You call getDepth with root, but root is never changed. So it is always the same. You should call for instance,

    return getDepth(currentMaxDepth, root.children[0]);
    

提交回复
热议问题