关于树结构递归取对象的函数,该如何编写呢?

已知树结构:

var data = [
    {
        id: 1,
        tid: 10,
        name: '祖先1',
        childList: [
            {
                id: 11,
                name: '父1',
                childList: [
                    {
                        id: 111,
                        name: '子1',
                        key: 111
                    }
                ]
            },
            {
                id: 12,
                name: '父2',
                childList: [
                    {
                        id: 112,
                        name: '子2',
                        key: 112
                    },
                    {
                        id: 113,
                        name: '子3',
                        key: 113
                    }
                ]
            }
        ]
    },
    {
        id: 2,
        tid: 11,
        name: '祖先2',
        childList: [
            {
                id: 11,
                name: '父3',
                childList: [
                    {
                        id: 114,
                        name: '子4',
                        key: 114
                    },
                    {
                        id: 115,
                        name: '子5',
                        key: 115
                    }
                ]
            },
            {
                id: 12,
                name: '父4',
                childList: [
                    {
                        id: 116,
                        name: '子6',
                        key: 116
                    },
                    {
                        id: 117,
                        name: '子7',
                        key: 117
                    }
                ]
            }
        ]
    }
]

编写一个js函数 传入 key 的值 114,返回 114 所在子节点对应的祖先节点对象
例如:

{
    id: 2,
    tid: 11,
    name: '祖先2',
    childList: [
        {
            id: 11,
            name: '父3',
            childList: [
                {
                    id: 114,
                    name: '子4',
                    key: 114
                },
                {
                    id: 115,
                    name: '子5',
                    key: 115
                }
            ]
        },
        {
            id: 12,
            name: '父4',
            childList: [
                {
                    id: 116,
                    name: '子6',
                    key: 116
                },
                {
                    id: 117,
                    name: '子7',
                    key: 117
                }
            ]
        }
    ]
}
阅读 1.3k
2 个回答

一行流的话:

const getAncestor = (data, key) => ( data?.key === key ? data : data?.length && data.find(sub => getAncestor(sub, key)) || data?.childList?.find(child => getAncestor(child, key)) )

希望能帮助到你。

以前写过这样一个方法,你可以试试:

// 遍历树节点 获取当前节点的所有父节点及本身
export const getAllParents = (tree, func, path = []) => {
  if (!tree) return [];
  for (const data of tree) {
    path.push(data.id);
    if (func(data)) return path;
    if (data.children) {
      const findChildren = getAllParents(data.children, func, path);
      if (findChildren.length) return findChildren;
    }
    path.pop();
  }
  return [];
};
let arr = [];
// treesArr 是树结构完整数据
treesArr.forEach((item) => {
  let result = getAllParents(
    treeData.value,
    (node) => node.id === item
  );
  console.log(item + "的父级及自己:", result);
  arr = arr.concat(result);
});
// console.log("menuIds getParentKey", arr);
const resArr = [...new Set(arr)]; //去重

抛砖引玉了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题