首先定义个数组
const arr = [
{
value: "2973",
label: "陕西",
children: [
{
value: "2974",
label: "西安",
children: [
{ value: "2975", label: "西安市", isLeaf: true },
{ value: "2976", label: "高陵县", isLeaf: true }
]
},
{
value: "2980",
label: "铜川",
children: [
{ value: "2981", label: "铜川市", isLeaf: true },
{ value: "2982", label: "宜君县", isLeaf: true }
]
}
]
}
]
然后我们首先用map进行递归/结构我们需要的结构
const recursionFun = (list): any => {
return (list || []).map((item, index: number) => {
if (item.children && (item.children || []).length > 0) {
item = {
value: item?.id,
label: item?.categoryName,
children : cateList(item.children)
}
return item
}
return {
value: item?.value,
label: item?.label,
};
});
};
const recursionResult = recursionFun(arr)
也可以用forEach进行递归我们需要的数据结构
const recursionFun = (list) => {
(list || []).forEach((item, index) => {
if (item.children && (item.children || []).length > 0) {
dataSource(item.children)
}
item.label = item.label
item.value = item.value
})
}
const recursionResult = recursionFun(arr)