我正在尝试读取一个如下所示的 JSON 文件:
[ {
"parent" : "cmeta_transform",
"path" : "hcat://sample/dt=yyyyMMddHH",
"child" : "lite_video_hourly"
} ]
然后,我正在迭代内容并从data.forEach
函数调用内部设置节点和边缘(下面的代码)
g
但是,当我迭代它时,似乎没有设置变量。如果我在匿名函数之外执行它,它会起作用。我错过了什么?
<script id="js">
// Create a new directed graph
var g = new dagreD3.graphlib.Graph().setGraph({});
d3.json("expectedOutput.json", function(data) {
data.forEach(function (job) {
console.log(job); // I do see job values correctly.
g.setNode(job.child, { label: job.child });
g.setEdge(job.parent, job.child, { label: job.path });
});
});
console.log(g); // g doesn't have the nodes & edges values set above
// if I do this
g.setNode(job.child, { label: job.child });
g.setEdge(job.parent, job.child, { label: job.path });
console.log(g); // I see g is set properly
</script>