2 回答
TA贡献1895条经验 获得超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);
TA贡献1798条经验 获得超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 [];
}
};
添加回答
举报