根据数据中最深的一个值,找出这个值有关联的整个树,比如地址的回显,接口会给出最后面的地址,需要根据这个值去循环上面树的所有节点的lable值

/**
* 根据接口返回地址字段,找出地址树中的森林
* @param {*} value 接口给出的字段值
* @param {*} key 接口中所对应的key值
* @param {*} arr 数据源
*/
function getAddressPath(value, key, arr){
    if(!value) return false;
        let temppath = [];
       try {
          function getNodePath(node){
              temppath.push(node);
              //找到符合条件的节点,通过throw终止掉递归
              if (node[key] === value) {
                 throw ("GOT IT!");
               }
             if (node.children && node.children.length > 0) {
                for (var i = 0; i < node.children.length; i++) {
                   getNodePath(node.children[i]);
                }
              //当前节点的子节点遍历完依旧没找到,则删除路径中的该节点
              temppath.pop();
             } else {
             //找到叶子节点时,删除路径当中的该叶子节点
               temppath.pop();
          }
      } 
         for (let i = 0; i < arr.length; i++) {
           getNodePath(arr[i]);
        }
      } catch (e) {
      temppath;
      }
        const _label = temppath && temppath.map((item) => item.label);
        const label_str = _label.join('/');
        return label_str;
}
posted @ 2019-11-18 09:30  凹润之之之  阅读(96)  评论(0编辑  收藏  举报