I have a background in functional programming and understand recursion in principle, but I can't seem to translate this knowledge into the D3.js environment.
I have a hello world script below which attempts to simply print the contents of a nested data structure. Following advice on other threads, I can use .filter
to return just the nodes, but how do I continue this example to recursively print the nested items?
<!DOCYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="d3.v3.js"></script>
<script>
function draw(data)
{
"use strict";
d3.select("body")
.selectAll("p")
.data(data)
.enter()
.append("p")
.text(function(d) {
if (d instanceof Array) {
return "WHAT DO I PUT HERE?";
}
else {
return d;
};
});
}
</script>
</head>
<body>
Hello world
<script>
draw([1, [2, [1, 2, 3, 4] ], 3, 4, 5]);
</script>
</body>
</html>