JavaScript之递归查找所有父节点

2021-03-13 01:29

阅读:826

......

data: () => ({
  // 数据 dt: [{ id: ‘1‘, children: [ { id: ‘1-1‘, children: [ { id: ‘1-1-1‘, children: [] } ] }, { id: ‘1-2‘, children: [ { id: ‘1-2-1‘, children: [] } ] } ] }, { id: ‘2‘, children: [ { id: ‘2-1‘, children: [ { id: ‘2-1-1‘, children: [{ id: ‘2-1-1-1‘, children: [] }] } ] } ] }], path: [],// 保存递归到的每一层路径 curPath: [], // 当前路径 curId: ‘2-1-1‘, // 目标节点 isRecursion: true // 是否已找到目标几点 }), methods: { handleTestClick () { const { dt } = this this.path = [] dt.forEach((item, index) => { this.path[index] = [item.id] // 将每一层的路径保存下来 this.recursion(item.children, index) }) console.log(this.path, this.curPath) }, recursion (ary, index) { for (let i = 0; i

......


评论


亲,登录后才可以留言!