通過(guò)遞歸生成樹(shù)形結(jié)構(gòu)數(shù)據(jù)

如下數(shù)據(jù):

[
    {
        "id": 1,
        "name": "11111111樓",
        "parentId": 0
    },
    {
        "id": 2,
        "name": "2樓",
        "parentId": 0
    },
    {
        "id": 4,
        "name": "1樓兒子2",
        "parentId": 1
    },
    {
        "id": 6,
        "name": "你是誰(shuí)啊",
        "parentId": 4
    },
    {
        "id": 12,
        "name": "566666",
        "parentId": 2
    },
    {
        "id": 13,
        "name": "33333樓",
        "parentId": 0
    },
    {
        "id": 14,
        "name": "7567566765",
        "parentId": 13
    },
    {
        "id": 15,
        "name": "1312",
        "parentId": 6
    }
]

生成樹(shù)結(jié)構(gòu)代碼:

 load() {
                loadCatOptions().then(res => {//發(fā)送請(qǐng)求獲取數(shù)據(jù)
                    this.catOptions = this.generateOptions(res);
                    console.log(this.catOptions);
                })
            },
 
            generateOptions(params) {//生成Cascader級(jí)聯(lián)數(shù)據(jù)
                var result = [];
                for (let param of params) {
                    if (param.parentId == 0) {//判斷是否為頂層節(jié)點(diǎn)
                        var parent = {//轉(zhuǎn)換成el-Cascader可以識(shí)別的數(shù)據(jù)結(jié)構(gòu)
                            'label': param.name,
                            'value': param.id
                        }
                        parent.children = this.getchilds(param.id, params);//獲取子節(jié)點(diǎn)
                        result.push(parent);
                    }
                }
                return result;
            },
 
            getchilds(id, array) {
                let childs = new Array();
                for (let arr of array) {//循環(huán)獲取子節(jié)點(diǎn)
                    if (arr.parentId == id) {
                        childs.push({
                            'label': arr.name,
                            'value': arr.id
                        });
                    }
                }
                for (let child of childs) {//獲取子節(jié)點(diǎn)的子節(jié)點(diǎn)
                    let childscopy = this.getchilds(child.value, array);//遞歸獲取子節(jié)點(diǎn)
                    console.log(childscopy)
                    if (childscopy.length > 0) {
                        child.children = childscopy;
                    }
                }
                return childs;
            },

轉(zhuǎn)換后的數(shù)據(jù):

[
    {
        "children": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "label": "1312",
                                "value": 15
                            }
                        ],
                        "label": "你是誰(shuí)啊",
                        "value": 6
                    }
                ],
                "label": "1樓兒子2",
                "value": 4
            }
        ],
        "label": "11111111樓",
        "value": 1
    },
    {
        "children": [
            {
                "label": "566666",
                "value": 12
            }
        ],
        "label": "2樓",
        "value": 2
    },
    {
        "children": [
            {
                "label": "7567566765",
                "value": 14
            }
        ],
        "label": "33333樓",
        "value": 13
    }
]

根據(jù)樹(shù)形數(shù)據(jù)生成樹(shù)形文檔

function g(count, title) {
  return (
    Array(count - 1)
      .fill("----")
      .join("") + title
  );
}
let txt = "";
function loop(menus, count) {
  count++;
  for (const item of menus) {
    item.count = count;
    txt += g(item.count, item.title) + '----' + item.key + "\n";
    loop(item.children || [], item.count);
  }
}
loop(menu, 0);

fs.writeFileSync("./1.txt", txt);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容