1

I am trying to create a menuTree from fetched arrays in React. My problem is that I don't know how to build my array recursively:

Lets say I do a fetch of a mainUrl and get the array:

[
 {"id":"EU","type":"l","text":"Europe"},
 {"id":"AS","type":"l","text":"Asia"}
]

Since type is "l" i need to do another fetch: mainUrl/EU

Now i get:

[
 {"id":"SP","type":"l","text":"Spain"},
 {"id":"FR","type":"l","text":"France"}
]

Again both types are "l" so I do another fetch: mainUrl/EU/SP

Now i get:

[
 {"id":"MA","type":"t","text":"Madrid"}
]

Since type is now "t" i stop and start again with France and then Asia.

Keeping in mind that i don't know the dept of they children in the array

The my array should look like this:

[
    {
        "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":[...]
    }
]
3
  • Have you tried anything so far? Commented Oct 8, 2019 at 12:34
  • Hi, Yes, many things, my main problem is that i don't know how to push a child array inside a child object recursively. Then I also nested foreach loops that I got working, but I am trying to figure out how to do it recursively so that the dept of children dose'nt matter. Commented Oct 8, 2019 at 12:39
  • I know how to print the menuTree array out recursively, Just not how to build the array itself Commented Oct 8, 2019 at 12:40

2 Answers 2

2
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);
Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure that will work? What about when the URL needs to be mainUrl/EU/SP?
well, i changed "mainUrl + / + id" by "url + / + id", it will work then
Thank you so much Maxime Girou! It with worked like a charm
1
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);
            }
        });

and fetch is like this:

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 [];
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.