美文网首页
树形结构,通过子节点,获取祖先节点

树形结构,通过子节点,获取祖先节点

作者: 芸芸众生ing | 来源:发表于2022-11-03 16:05 被阅读0次
function getValuePath(list , val ) {
      const { value, children } = this.PROPS;
      const arr = [];
      getObject(list, 0);
      function getObject(listObj , idx )  {
        let bool = false;
        for (let item of listObj) {
          if (item[value] === val) {
            bool = true;
            arr[idx] = item;
            break;
          } else if (item[children]?.length) {
            bool = getObject(item[children], idx + 1);
            if (bool) {
              arr[idx] = item;
              break;
            }
          }
        }
        return bool;
      }
      return arr;
    },

相关文章

网友评论

      本文标题:树形结构,通过子节点,获取祖先节点

      本文链接:https://www.haomeiwen.com/subject/psqutdtx.html