D3.js node.ancestors() Function

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>     <metacharset="utf-8">  Â    <scriptsrc=     </script> </head>  Â<body>     <script>         var data = {             "name":"zambiatek",              "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>     <metacharset="utf-8">  Â    <scriptsrc=     </script> </head>  Â<body>     <script>         var data = {"name":"GFG1"}  Â        var root = d3.hierarchy(data);  Â        console.log(root.ancestors());     </script> </body>  Â</html> | 
Output:
 
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
 
				 
					



