Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

D3.js node.ancestors() Function

  • Last Updated : 23 Sep, 2020

The node.ancestors() function returns an array of ancestors nodes, starting with the given node, then followed by each parent up to the root.

Syntax:

node.ancestors();

Parameters: This method takes no parameters.

Return Values: This method returns an array of ancestors nodes.

Example 1:

HTML




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

Output:

Example 2: 

HTML




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

Output:
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!