2
我有一個CSV表示分層樹數據:D3.js - 條件適用nest.key()函數數組元素
industry,level1,level2,level3,name
Ecommerce,Web,,,Rakuten
Ecommerce,Crowdsourcing,,,Lancers
Social,Photo sharing,Deco apps,,Snapeee
Social,Photo sharing,Deco apps,Collage apps,DecoAlbum
Portals,,,,Yahoo Japan
行level1...level3
表示子節點,和行name
表示底部節點。我正在嘗試應用d3.nest()
函數,以獲得分層的JSON對象。特別是,我想擺脫層級行爲空的節點。到目前爲止,我下面的代碼:
d3.csv("data.csv", function(rows) {
sunburst_tree = d3.nest()
.key(function(d) { return d.industry; })
.key(function(d) { return d.level1; })
.key(function(d) { if (!(typeof d.level2 === 'undefined')) return d.level2; })
.entries(rows);
console.log(sunburst_tree);
});
將會產生JSON對象空鍵,這樣的:
{"key":"Portals",
"values":[{"key":"",
"values":[{"key":"",
"values":[{"industry":"Portals","level1":"","level2":"","level3":"","name":"Yahoo Japan"}]
}]
}]
}
相反,我想刪除所有空的子節點:
{"key":"Portals",
"values":[{"industry":"Portals",
"level1":"","level2":"","level3":"","name":"Yahoo Japan"}]}
}
如何做到這一點?