【工具函数】数组 找到链条中所有父级的id
<h4>已知id找到链条中所有的id</h4>
<hr />
<p>已知数据格式,实现一个函数 fn 找出链条中所有的父级 id</p>
<h5>题目:</h5>
<pre><code>1.是否等于第一层
2.已知id是否包含第一层开头id
包含第一层id+递归</code></pre>
<pre><code>const list = [{
id: '1',
name: 'test1',
children: [
{
id: '11',
name: 'test11',
children: [
{
id: '111',
name: 'test111'
},
{
id: '112',
name: 'test112'
}
]
},
{
id: '12',
name: 'test12',
children: [
{
id: '121',
name: 'test121'
},
{
id: '122',
name: 'test122'
}
]
}
]
}];
const id = '112'
const fn = (value) => {
...
}
fn(id, list) // 输出 [1, 11, 112]</code></pre>