文章 评论 浏览 27
const arr = [ { id: 1, name: '广东', children: [ { id: 11, name: '深圳', children: [ { id: 111, name: '南山区' }, { id: 112, name: '福田区' } ] } ] } ] const id = 112 const fn = (treeData, idsList = [], ids = []) => { treeData.forEach(item => { const { id, children } = item const tempIds = [].concat(ids) tempIds.push(id) idsList.push(tempIds); if (children && !!children.length) { fn(children, idsList, tempIds); } }); return idsList; }; const list = fn(arr) const result = list.filter(item => { const lastValue = item[item.length - 1] return Number(lastValue) === Number(id) })
思路:1、将数组转换为多个一维数组,结果如下所示:2、循环第一步所得的结果,判断最后一位是不是和要查询的id相同,如果是那么就返回结果
https://www.jianshu.com/p/54cc04190252浏览器的缓存机制
这篇完美~
文章 0 评论 0
接受
思路:

1、将数组转换为多个一维数组,结果如下所示:
2、循环第一步所得的结果,判断最后一位是不是和要查询的id相同,如果是那么就返回结果
第 92 题:已知数据格式,实现一个函数 fn 找出链条中所有的父级 id