我已经阅读了很多解决这个问题的方法,例如我正在尝试使用array-to-tree
鉴于此数据:
[
{
"_id": 33,
"parent": null,
"name": "Wealth and Investment Management and Insurance",
"code": "wm-0001",
"__v": 0
},
{
"_id": 34,
"parent": null,
"name": "Corporate and Investment Banking",
"code": "cib-0001",
"__v": 0
},
{
"_id": 35,
"parent": 33,
"name": "WIMI Business Unit 1",
"code": "WIMBU-0001",
"__v": 0
}
]
并使用此代码
var arrayToTree = require('array-to-tree');
var tree = arrayToTree(data, {
parentProperty: 'parent',
customID: '_id'
});
我无法理解为什么它的孤儿我的孩子?即我回来了
[ { _id: 33,
parent: null,
name: 'Wealth and Investment Management and Insurance',
code: 'wm-0001',
__v: 0 },
{ _id: 34,
parent: null,
name: 'Corporate and Investment Banking',
code: 'cib-0001',
__v: 0 } ]
答案 0 :(得分:1)
如果array-to-tree
的类型为_id
,Number
似乎不知道该怎么办;
更改此项将平面数组转换为树形结构。
如果它们是_id
,您可以遍历数组以将键 parent
/ string
转换为number
;
如果你要求它,我只会编辑它(但上面的解决方案)