La función node.descendants() en la biblioteca d3.js se usa para generar y devolver una array de Nodes descendientes.
Sintaxis:
node.descendants();
Parámetros: Esta función no acepta ningún parámetro.
Valores devueltos: esta función devuelve una array.
A continuación se dan algunos ejemplos de la función dada anteriormente.
Ejemplo 1:
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" path1tent= "width=device-width, initial-scale = 1.0"/> <script src="https://d3js.org/d3.v4.min.js"> </script> </head> <body> <script> var obj = d3.hierarchy({ name: "rootNode", children: [ { name: "child1" }, { name: "child2", children: [ { name: "grandChild1" }, ] }, { name: "child3", children: [ { name: "grandChild5" }, { name: "grandChild6" }, { name: "grandChild7" }, { name: "grandChild8" }, ] } ] }); // Descendant of child3 console.log("Descendant of child3 are: "); console.log(obj.children[2] .descendants()[0].data.name); console.log(obj.children[2] .descendants()[1].data.name); console.log(obj.children[2] .descendants()[2].data.name); console.log(obj.children[2] .descendants()[3].data.name); console.log(obj.children[2] .descendants()[4].data.name); </script> </body> </html>
Producción:
Ejemplo 2:
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" path1tent= "width=device-width, initial-scale = 1.0"/> <script src="https://d3js.org/d3.v4.min.js"> </script> </head> <body> <script> var obj = d3.hierarchy({ name: "rootNode", children: [ { name: "child1" }, { name: "child2", children: [ { name: "grandChild1" }, ] }, { name: "child3", children: [ { name: "grandChild5" }, { name: "grandChild6" }, { name: "grandChild7" }, { name: "grandChild8" }, ] } ] }); // Descendant of child3 console.log("Descendant of child3 are: "); console.log(obj.children[2].descendants()); </script> </body> </html>
Producción: