JS数组对象:通过子id找所有父id

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    </body>
    <script type="text/javascript">
        const city = [{
            companyId: 1,
            id: "1",
            label: "四川省",
            parentId: "",
            value: "1",
            children: [{
                companyId: 1,
                id: "2",
                label: "成都市",
                parentId: "1",
                value: "2",
                children: [{
                    companyId: 1,
                    id: "3",
                    label: "高新区",
                    parentId: "2",
                    value: "3",
                    children: [{
                        companyId: 1,
                        id: "4",
                        label: "环球中心",
                        parentId: "3",
                        value: "4"
                    }]
                }]
            }]
        }]
        const city2 = [
            {
                companyId: 1,
                id: "0",
                label: "江西省",
                parentId: "-99",
                value: "0",
            },
            {
                companyId: 1,
                id: "1",
                label: "四川省",
                parentId: "-99",
                value: "1",
            },
            {
                companyId: 1,
                id: "2",
                label: "成都市",
                parentId: "1",
                value: "2",
            },
            {
                companyId: 1,
                id: "3",
                label: "高新区",
                parentId: "2",
                value: "3",
            },
            {
                companyId: 1,
                id: "4",
                label: "环球中心",
                parentId: "3",
                value: "4"
            }
        ]
        // 扁平数据转换树状数据
        function buildTree(list, rootID = "0") {
            let root = null;
            if (list && list.length) {
                root = {
                    id: rootID,
                    parentId: null,
                    children: []
                };
                const group = {};
                for (let index = 0; index < list.length; index += 1) {
                    if (list[index].parentId !== null && list[index].parentId !== undefined) {
                        if (!group[list[index].parentId]) {
                            group[list[index].parentId] = [];
                        }
                        group[list[index].parentId].push(list[index]);
                    }
                }
                const queue = [];
                queue.push(root);
                while (queue.length) {
                    const node = queue.shift();
                    node.children = group[node.id] && group[node.id].length ? group[node.id] : [];
                    if (node.children) {
                        Array.prototype.push.apply(queue, node.children);
                    }
                }
            }
            return root;
        }
        // 通过id找父id(包括自己)
        function getParentIdList(array, id) {
            let parentArray = [];

            if (array.length === 0) {
                return parentArray;
            }

            const recursion = function (arrayNew, id) {
                for (let i = 0; i < arrayNew.length; i++) {
                    let node = arrayNew[i];
                    if (node.id === id) {
                        parentArray.unshift(id);
                        recursion(array, node.parentId);
                        break;
                    } else {
                        if (!!node.children) {
                            recursion(node.children, id);
                        }
                    }
                }
                return parentArray;
            }
            let arrayNew = array;
            parentArray = recursion(arrayNew, id);
            return parentArray;
        }
        let arr = buildTree(JSON.parse(JSON.stringify(city2)), '-99')
        console.log('arr', arr)
        console.log('getParentIdList', getParentIdList(arr.children, '3'))
    </script>
</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容