树型json结构相关方法合集

1. 通过当前子id查找所有父级id
 findParentIds(tree, targetId, parentIds = []) {
        for (let node of tree) {
          if (node.id === targetId) {
            return parentIds;
          }
          if (node.children && node.children.length > 0) {
            const parentsHere = [...parentIds, node.id];
            const result = findParentIds(node.children, targetId, parentsHere);
            if (result) return result;
          }
        }
        return null;
    }

调用方式:
findParentIds(trees, currentNode?.id)
2. 通过children列表获取本身以及下面所有的children,输出所有children数组
getAllChildren(items) {
        if(!items || items?.length === 0) return [];
        return items.reduce((acc, item) => {
            const { children = [], ...rest } = item;
            return [...acc, rest, ...this.getAllChildren(children)];
        }, []);
    }
    调用: getAllChildren( children 数组)
3.根据id深度搜索节点
deepQuery = (tree, id) => {
        let isGet = false
        let retNode = null
        function deepSearch(tree, id){
            for (let i = 0; i < tree.length; i++) {
                if (tree[i].children && tree[i].children.length > 0) {
                    deepSearch(tree[i].children, id)
                }
                if (id === tree[i].id || isGet) {
                    isGet || (retNode = tree[i])
                    isGet = true
                    break
                }
            }
        }
        deepSearch(tree, id)
        return retNode
    }
  调用: deepQuery(trees, '18301012232100608');
4.根据id列表,获取所有子节点的id列表
getChildrenId = (ids) => {
        let result = [];
        const { trees } = this.state;
        ids.forEach(ele => {
            let current = this.deepQuery(trees, ele);
            let allChildren = this.getAllChildren(current?.children);
            if(allChildren.length !== 0) {
                result.push(...allChildren);
            }
            
        });
        return _.uniqBy(result, "id");
    };
5. id数组转换成树结构json
arrayToTree = (list, root) => {
        return list
        .filter(item => item.parentId === root)
        .map(item => ({ ...item, children: this.arrayToTree(list, item.id) }))
    }
    let treeData = []
            let children = [];
            treeData = [
                {
                    name:'全部',
                    key:'isAll',
                    id: '0',
                    parentId: '-1',
                    children: [...this.arrayToTree(data, '0')]
                }
       ];
 [
    {
        "id": "1111110021",
        "parentId": "1111110004",
        "layer": 2,
        "name": "二级分类",
        "children": []
    },
    {
        "id": "1111110031",
        "parentId": "1111110021",
        "layer": 3,
        "name": "三级分类",
        "children": []
    },
    {
        "id": "1111110004",
        "parentId": "0",
        "layer": 1,
        "name": "jml",
        "children": []
    },
    {
        "id": "1111110005",
        "parentId": "0",
        "layer": 1,
        "name": "test5566",
        "children": []
    },

]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值