将一层数据处理成树形结构

convert(list){
        const map = list.reduce((res,v)=>{
          res[v.id] = v;
          return res;
        },{})
        const res = [];
        for(let item of list){
          if(item.parentId === 0){
              res.push(item);
              continue;
          }
          if(item.parentId in map){
              const parent = map[item.parentId];
              parent.children = parent.children || [];
              parent.children.push(item);
          }
        }
        return res;
      },
上一篇:ReactNative Day4


下一篇:Koltin16,androidstudio蓝牙应用开发