以下是原始数组,我想要转换它的结构,它与antd的列结构相同:
const base = [
{
name: "Jonh"
},
{
name: "jane"
},
{
name: "Tom",
children: [
{
name: "Jery"
},
{
name: "Mart"
},
{
name: "Mary",
children: [
{
name: "bob"
}
]
}
]
},
{
name: "boboy"
}
]
我想转换成这样的数组:
我的简单愿望是将子元素提升到与父数组相同的层级,同时保持父级结构不变。
const result = [
{
name: "Jonh"
},
{
name: "jane"
},
{
name: "Tom",
children: [
{
name: "Jery"
},
{
name: "Mart"
},
{
name: "Mary",
children: [
{
name: "bob"
}
]
}
]
},
[
{
name: "Jery"
},
{
name: "Mart"
},
{
name: "Mary",
children: [
{
name: "bob"
}
]
},
[
{
name: "bob"
}
],
],
{
name: "boboy"
}
]
对某些人来说这可能很简单,但我是新手,希望各位能帮忙。
这是我尝试的方法:
const filterChild = (listColumn: IColumns[]): any[] => {
return listColumn.map((col) => {
return col.children ? filterChild(col.children) : col;
});
};
进行此操作的目的是我正在尝试创建一个带有“表头分组”功能的自定义表格,类似于antd的表格组件。