处理数组对象层级,树形数据(递归)
数据范例
let test = {
hasChild: true,
children: [
{
hasChild: false,
children: [],
},
{
hasChild: true,
children: [
{
hasChild: true,
children: [
{
hasChild: true,
children: [
{
hasChild: true,
children: [
{
hasChild: false,
children: [],
}
],
}
],
}
],
},
{
hasChild: false,
children: [],
}
],
}
],
};
let handleTest = this.handleChildDeep(test.children)
handleChildDeep(children) {
let _children = children
if(_children.length === 0) {
return []
} else {
_children.forEach(el => {
if (!el.hasChild) delete el.children
else this.handleChildDeep(el.children)
})
return _children
}
},