需求如上 现在有数据如下
data= [
{
id: 0,
event: '事件1',
timeLine: 50,
comment: '无',
age:13
},
{
id: 1,
event: '事件1',
timeLine: 100,
comment: '无',
age:13
children: [
{
id: 2,
event: '事件2',
timeLine: 10,
comment: '无',
age:11
},
{
id: 3,
event: '事件3',
timeLine: 90,
comment: '无',
age:13
children: [
{
id: 4,
event: '事件4',
timeLine: 5,
comment: '无',
age:17
},
{
id: 5,
event: '事件5',
timeLine: 10,
comment: '无',
age:13
},
{
id: 6,
event: '事件6',
timeLine: 75,
comment: '无',
age:13
children: [
{
id: 7,
event: '事件7',
timeLine: 50,
comment: '无',
age:13
children: [
{
id: 71,
event: '事件71',
timeLine: 25,
comment: 'xx',
age:18
},
{
id: 72,
event: '事件72',
timeLine: 5,
comment: 'xx',
age:13
},
{
id: 73,
event: '事件73',
timeLine: 20,
comment: 'xx',
age:15
}
]
},
{
id: 8,
event: '事件8',
timeLine: 25,
comment: '无',
age:19
}
]
}
]
}
]
}
]
现在希望根据age是13的属性 对data进行过滤,取得新的树形结构,求一个递归方法
用递归写了个,人工核对了一下输出似乎没有问题。不排除在使用其他数据的时候会发生错误。反正思路在,如果有错改改应该不困难。
输出结果太长,用一个示意的树来表示: