问题遇到的现象和发生背景
element动态树的数据一共有四层,如何遍历数据把最后一层children数据去掉呢?
我想要达到的结果
遍历出的数据保留三层
element动态树的数据一共有四层,如何遍历数据把最后一层children数据去掉呢?
遍历出的数据保留三层
判断 层数呗,最笨的就是循环三遍
let treeData = [
{
name: "555",
children: [
{
name: "666",
children: [
{
name: "777",
children: [
{
name: "888",
children: [
{
name: "5555",
children: []
}
]
}
]
}
]
}
]
},
{
name: "111",
children: [
{
name: "222",
children: [
{
name: "333",
children: [
{
name: "444",
children: [
{
name: "5555",
children: []
}
]
}
]
}
]
}
]
}
];
console.log(treeDataFn(treeData))
function treeDataFn(treeData) {
treeData.map((item, index) => {
if (item.children) {
item.children.map((item1) => {
if (item1.children) {
item1.children.map((item2) => {
delete item2.children;
})
}
})
}
});
return treeData;
}