![](/img/trans.png)
[英]Convert nodes that consists of nested objects, strings, array into an unordered list (Javascript)
[英]Javascript: Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure
我已經閱讀了許多解決此問題的方法,例如正在嘗試使用數組到樹
給定此數據:
[
{
"_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 } ]
似乎如果_id
為Number
類型,則array-to-tree
不知道該怎么做;
更改此選項會將平面數組轉換為樹結構。
您可以遍歷數組將鍵 _id
/ parent
轉換為string
如果它們是number
;
如果您需要,我只會對其進行編輯(但以上方法可以解決)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.