1
我正在從mongodb .find()中檢索記錄,並嘗試使用d3v4 .stratify()嵌套這些記錄。不幸的是,我得到錯誤使用d3v4嵌套JSON .stratify()
Error: ambiguous: Product Line 1
我假設我誤解了API documentation。
雖然這個問題似乎接近this post,我不認爲這是完全一樣的。但是,我試圖得到的結果是like this。
有人可以幫助我理解如何正確使用.stratify()嗎?或建議一個更簡單的方法來做到這一點?
我的結果集(mongodb_data
)不是很大(也許〜2500級的產品)。
我的平坦mongodb_data
看起來像這樣,
[
{
"name": "Product Line 1",
"reference": "product 1.A identifier"
},
{
"name": "Product Line 1",
"reference": "product 1.B identifier"
},
{
"name": "Product Line 2",
"reference": "product 2.A identifier"
},
{
"name": "Product Line 2",
"reference": "product 2.B identifier"
}
];
所需分層/嵌套數據需要像這樣,
{
"name": "Master Product Catalog",
"children": [
{
"name": "Product Line 1",
"children": [
{ "name": "product 1.A identifier" },
{ "name": "product 1.B identifier" }
]
},
{ "name": "Product Line 2",
"children": [
{ "name": "product 2.A identifier" },
{ "name": "product 2.B identifier" }
]
}
]
}
我已經使用例如由API文檔被如下,
var stratdata = d3.stratify()
.id(function(d) { return d.name; })
.parentId(function(d) { return d.name; })
(mongodb_data);
嗯,有趣。它適用於我的