D3.js node.depth Property

The D3.js node.depth property returns the length of the path from the node up to the root.

Syntax:

node.depth

Return Value: This property returns the length of the path from the node up to the root.

Example 1:

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
        "https://d3js.org/d3.v5.min.js">
    </script>
</head>
  
<body>
    <script>
        var data = {
            "name":"GeeksforGeeks", 
            "about":"Computer Science Portal",
            "children":[
                {"name":"GFG1"},
                {"name":"GFG2"},
                {"name":"GFG3"},
                {"name":"GFG4"}
            ]
        }
        var root = d3.hierarchy(data);
        console.log(root.children[0].depth);
    </script>
</body>
  
</html>

chevron_right


Output:



Example 2: In this example, this method returns undefined as the root node has no child node.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
  
    <script src=
        "https://d3js.org/d3.v5.min.js">
    </script>
</head>
  
<body>
    <script>
        var data = {"name":"GFG1"}
  
        var root = d3.hierarchy(data);
  
        console.log(root.depth);
    </script>
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.