递归简化数据 处理前的数据
[
{
“guid”: “C81242A4E81A278BE0501AACD4C71F2D”,
“code”: “01”,
“name”: “预算科”,
“children”: [
{
“guid”: “C81397AB80A17B29E0501AACD4C7622A”,
“code”: “907”,
“name”: “包头市财政局(预算)”,
“children”: [
{
“guid”: “C81397AB80A27B29E0501AACD4C7622A”,
“children”: [],
“code”: “907001”,
“name”: “预算科本级”,
}
]
}
],
}
]
处理方式
getMiniNodes(array) {
const filterList = []
array.forEach((ele) => {
const { children, guid } = ele
if (children && children.length) {
const currentEle = {
children: [],
guid
}
const result = this.getMiniNodes(children)
if (result.length) {
currentEle.children = result
filterList.push(currentEle)
}
} else {
filterList.push({ guid })
}
})
return filterList
}
}```
处理后的数据
[
{
"guid": "C81242A4E81A278BE0501AACD4C71F2D",
"children": [
{
"guid": "C81397AB80A17B29E0501AACD4C7622A",
"children": [
{
"guid": "C81397AB80A27B29E0501AACD4C7622A",
}
]
}
]
}
]
递归数据过滤(例如过滤最底层掉符合某个条件的数据)
```html
const filterMember = (key, data) => {
const filterList = [];
data.forEach((ele) => {
const { member, ...other } = ele;
if (member && member.length) {
const currentEle = {
member: [],
...other
};
const result = filterMember(key, member);
if (result.length) {
currentEle.member = result;
filterList.push(currentEle);
}
} else if (other.age > key) {
filterList.push(other);
}
});
return filterList;
}
处理后的数据(过滤掉了最底层的一个数据)
[
{
"guid": "C81242A4E81A278BE0501AACD4C71F2D",
"children": [
{
"guid": "C81397AB80A17B29E0501AACD4C7622A",
"children": [], //过滤掉的数据
"code": "907",
"name": "包头市财政局(预算)",
}
],
"code": "01",
"name": "预算科",
}
]