第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Reactjs 中遞歸構建數(shù)組

在 Reactjs 中遞歸構建數(shù)組

湖上湖 2021-11-12 17:39:42
我正在嘗試從 React 中獲取的數(shù)組創(chuàng)建一個 menuTree。我的問題是我不知道如何遞歸地構建我的數(shù)組:假設我獲取了一個 mainUrl 并獲取了數(shù)組:[ {"id":"EU","type":"l","text":"Europe"}, {"id":"AS","type":"l","text":"Asia"}]由于類型是“l(fā)”我需要做另一個獲取:mainUrl/EU現(xiàn)在我得到:[ {"id":"SP","type":"l","text":"Spain"}, {"id":"FR","type":"l","text":"France"}]同樣,這兩種類型都是“l(fā)”,所以我再取一次:mainUrl/EU/SP現(xiàn)在我得到:[ {"id":"MA","type":"t","text":"Madrid"}]由于類型現(xiàn)在是“t”,我停下來重新開始,然后是法國,然后是亞洲。請記住,我不知道他們在陣列中的孩子的部門我的數(shù)組應該是這樣的:[    {        "id": "EU",        "type": "l",        "text": "Europe",        "children": [            {                "id": "SP",                "type": "l",                "text": "Spain",                "children":[                    {                        "id": "MA",                        "type": "t",                        "text": "Madrid"                    }                ]            },            {                "id": "FR",                "type": "l",                "text": "France",                "children": [                    {                        "id": "PA",                        "type": "t",                        "text": "Paris"                    }                ]            }        ]    },    {        "id": "AS",        "type": "l",        "text": "Asia",        "children":[...]    }]
查看完整描述

2 回答

?
人到中年有點甜

TA貢獻1895條經(jīng)驗 獲得超7個贊

const mainUrl = "yourMainUrl"; 


const fetchDataTree = async url => {


  // I assume you will handle the fetch with your own method

  let countryArr = await yourFetchFunction(url);


  for (let key in countryArr) {

   if (countryArr[key].type === "l") {

     countryArr[key].children = await fetchDataTree(url + "/" + countryArr[key].id)

   }

 }


 return countryArr

}


const yourDataTree = await fetchDataTree(mainUrl);


查看完整回答
反對 回復 2021-11-12
?
呼如林

TA貢獻1798條經(jīng)驗 獲得超3個贊

const results = fetch("");

        function getChildren(name){

            const fetchData = fetch(name);

            fetchData.forEach(item => {

                if (item.type === "l") {

                    item.children = getChildren(item.id);

                }

            });

            return fetchData;

        }


        results.forEach(item => {

            if (item.type === "l") {

                item.children = getChildren(item.id);

            }

        });

和 fetch 是這樣的:


function fetch(u) {

    switch (u){

        case "":

            return [

                {

                    id: "EU",

                    type: "l",

                    text: "Europe"

                },

                {

                    id: "AS",

                    type: "l",

                    text: "Asia"

                }

            ]

        case "EU":

            return [

                {

                    id:"SP",

                    type:"l",

                    text:"Spain"

                },

                {

                    id:"FR",

                    type:"l",

                    text:"France"

                }

            ];

        case "SP":

            return [

                {

                    id:"MA",

                    type:"t",

                    text:"Madrid"

                }

            ];

        default:

            return [];

    }

};


查看完整回答
反對 回復 2021-11-12
  • 2 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號