在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ 樹形結(jié)構(gòu)已知子節(jié)點找父節(jié)點

樹形結(jié)構(gòu)已知子節(jié)點找父節(jié)點

已知 子節(jié)點 求父節(jié)點
例如 已知 AAA
希望 得到 A-AA-AAA的格式

data3: [{

      id: 1,
      label: '一級 2',
      children: [{
        id: 3,
        label: '二級 2-1',
        children: [{
          id: 4,
          label: '三級 3-1-1'
        }, {
          id: 5,
          label: '三級 3-1-2',
          disabled: true
        }]
      }, {
        id: 2,
        label: '二級 2-2',
        disabled: true,
        children: [{
          id: 6,
          label: '三級 3-2-1'
        }, {
          id: 7,
          label: '三級 3-2-2',
          disabled: true
        }]
      }]
    }],
回答
編輯回答
枕頭人

上次遇到這個問題,估計和你的一樣,問了問后端大神,給我解決了,可是我不知道原理是什么。給你貼出代碼,你看看吧。

<script>
    let treeData = [
        {
            id: 1,
            sub: [
                {
                    id: 2,
                    sub: [
                        {
                            id: 12,
                            sub: [
                                {
                                    id: 13
                                },
                                {
                                    id: 14
                                }
                            ]
                        },
                        {
                            id: 3,
                            sub: [
                                {
                                    id: 4,
                                },
                                {
                                    id: 9,
                                    sub: [
                                        {
                                            id: 10,
                                        },
                                        {
                                            id: 11
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            id: 7,
                            sub: [
                                {id: 8},
                            ]
                        }
                    ]
                },
                {
                    id: 5,
                    sub: [
                        {id: 6},
                    ]
                }
            ]
        },
        {
            id: 100
        }
    ]

    let currentId = 10  // 當前id
    let arr = [] // 定義數(shù)組,存放當前id的直系父ids
    function getParentsIds(data) {
        for (let i = 0; i < data.length; i++) {
            let temp = data[i]
            if (temp.id == currentId) {
                arr.push(temp.id);
                return 1
            }
            if (temp && temp.sub && temp.sub.length > 0) {
                let t = getParentsIds(temp.sub)
                if (t == 1) {
                    arr.push(temp.id)
                    return 1
                }
            }
        }
    }
    getParentsIds(treeData)
    console.log(arr)

</script>
2017年6月3日 08:25
編輯回答
敢試

修改一下數(shù)據(jù)結(jié)構(gòu)即可,添加一個父id屬性
{id:2,label:'xxx',children:[],parentId:1}

2018年2月7日 18:04
編輯回答
孤島

目測lz遇到的問題是樹形選擇器,某個節(jié)點被選中后希望拿到該節(jié)點的層級信息,目前找到比較好的方案是拿到數(shù)據(jù)后遍歷一遍,在每一個節(jié)點上生成一個levelInfo字段,標識當前層級信息。

[{
    projectid: 110000,
    name: "一級 1",
    levelInfo: "110000"
    children: [{
        projectid: 110100,
        name: "二級 1-1",
        levelInfo: "110000-110100"
        children: [{
            projectid: 110101,
            name: "三級 1-1-1",
            children: null,
            levelInfo: "110000-110100-110101"
        }, {
            projectid: 110102,
            name: "三級 1-1-2",
            children: null,
            levelInfo: "110000-110100-110102"
        }]
    }, {
        projectid: 110200,
        name: "二級 1-2",
        levelInfo: "110000-110200"
        children: [{
            projectid: 110201,
            name: "三級 1-2-1",
            children: null,
            levelInfo: "110000-110200-110201"
        }, {
            projectid: 110202,
            name: "三級 1-2-2",
            children: null,
            levelInfo: "110000-110200-110202"
        }]
    }]
}]
// 生成該結(jié)構(gòu)的函數(shù),希望可以幫到你
function formatTree(arr, levelInfo = '') {
    return arr.map(item => {
        const newParent = levelInfo ? levelInfo + '-' + item.projectid : '' + item.projectid;
        const temp = {
            ...item,
            levelInfo: newParent
        };
        if (item.children) {
            temp.children = formatTree(item.children, newParent);
        }
        return temp;
    })
}
2017年6月20日 21:30
編輯回答
抱緊我
let find = (array, label) =>{
    let stack = [];
    let going = true;
    
    let walker = (array, label) => {
        array.forEach(item => {
            if (!going) return;
            stack.push(item['label']);
            if (item['label'] === label) {
                going = false;
            } else if (item['children']) {
                walker(item['children'], label);
            } else {
                stack.pop();
            }
        });
        if (going) stack.pop();
    }

    walker(array, label);

    return stack.join('-');
}

console.log(find(data, '三級 3-2-2'))
// 一級 2-二級 2-2-三級 3-2-2

應該是 DFS

2017年10月15日 08:54
編輯回答
澐染
const getParent = (data, target) => {

  const get = (children, target, record = []) => (
    children.reduce((result, { label, children: innerChildren }) => {
      if (label === target) {
        return [...record, target]
      }
      if (innerChildren) {
        return [...result, ...get(innerChildren, target, [...record, label])]
      }
      return result
    }, []))

  return get(data, target).join('-')
}

// 一級 2-二級 2-1-三級 3-1-2
const str = getParent(data, '三級 3-1-2')
2017年5月6日 16:49