2021-04-19-🧮🧮查找树节点中符合条件的子节点

2021-04-18  本文已影响0人  沐深

1.查找一个

let filterTreeId = (list, id)  => {
      let targetObj = null;
      for (const item of list) {
        if (item.id === id) {
          targetObj = item;
          break;
        }
        if (item.childList && item.childList.length) {
          targetObj = filterTreeId(item.childList, id);
        }
      }
      return targetObj;
    },

2.查找多个

let  filterTreeIdList = (List, id)  => {
      let setList = [];
      for (const item of List) {
        if (item.id === id) {
          setList.push(item);
        }
        if (item.childList && item.childList.length >= 1) {
          setList = [
            ...setList,
            ...filterTreeIdList(item.childList, id)
          ];
        }
      }
      return setList;
  }
上一篇下一篇

猜你喜欢

热点阅读