添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
// 1. 一次性拿出祖节点,剩余的都是子节点 arr. forEach ( element => { if (element. parent_catalog_id == '0' ) {                         fatherList. push (element);                         fatherId. push (element. id );                       } else {                         childList. push (element);                         childParentList. push ( parseInt (element. parent_catalog_id )); let drr = this . traversalTree (fatherId, fatherList, childList, childParentList); //console.log(drr); traversalTree ( fatherId, fatherList, childList, childParentList ) { let subFatherList = []; let subFatherId = []; let subChildList = []; let subChildParentList = []; // 2. 祖节点的id作为数组,从剩余的数组里找子节点, 找到的子节点将成为新的父级节点 childParentList. forEach ( ( item, index ) => { const num =  fatherId. indexOf (item); if (num > - 1 ) { if (!(fatherList[num]. children && fatherList[num]. children . length > 0 )) { fatherList[num]. children = []; fatherList[num]. children . push (childList[index]);                 subFatherList. push (childList[index]);                 subFatherId. push (childList[index]. id );               } else { // 3. 没有被筛选出来的,继续放在剩余数组,直到找到父级节点为止 subChildList. push (childList[index]);                 subChildParentList. push ( parseInt (childList[index]. parent_catalog_id )); if (subChildList. length > 0 ) { // 4. traversalTree函数返回的是被处理过的父级节点内容 let resultChildArr = this . traversalTree (subFatherId, subFatherList, subChildList, subChildParentList);               resultChildArr. forEach ( ( item, index ) => { const num =  fatherId. indexOf ( parseInt (item. parent_catalog_id ));                 fatherList[num]. children . some ( ( element, inx ) => { if (item. id == element. id ) {                  fatherList[num]. children [inx] =  item; return true ; return fatherList; 复制代码
分类:
前端
标签: