使用递归遍历树状结构数据

递归遍历树状结构

datas:[{
  id: 1,
  name: '分组1',
  expand: true,
  type:'group',
  editable:false,
  active:false,
  children: [{
    id: 2,
    expand: true,
    name: '相机1',
    type:'camera',
    editable:false,
    active:false,
  }]
},
  {
    id: 3,
    expand: true,
    name: '分组2',
    type:'group',
    editable:false,
    active:false,
    children: [
      {
        id: 5,
        expand: true,
        name: '分组2-1',
        type:'group',
        editable:false,
        active:false,
        children: [{
          id: 6,
          expand: true,
          name: '相机2-1',
          type:'camera',
          editable:false,
          active:false,
        },{
          id: 7,
          expand: true,
          name: '相机2-2',
          type:'camera',
          editable:false,
          active:false,
        }]
      }]
  }],

// 遍历树形结构
traverseSingle(item,paramsId){
  if(item.id==paramsId){
    console.log('========',item.id,paramsId)
  }
},
traverseTree(data,paramsId){
  const _this = this;
  if(!data){return;}
  for(let i=0;i0){
      _this.traverseTree(data[i].children,paramsId);
    }
  }
  // console.log('==id====',paramsId)
},

你可能感兴趣的:(js)