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);
嗯,有趣。它适用于我的