从子节点id查找子节点的父节点?

Find parent node from child from child node id?

我有一个对象数组,里面有多个子节点,如何从子对象id找到父对象。

 [
  {
    id: "a1",
    name: "apple",
    subGroups: [
      {
        id: "a2",
        name: "apple-a",
        subGroups: [
          {
            id: "a3",
            name: "apple-b",
            subGroups: [
              {
                id: "a4",
                name: "apple-c",
                subGroups: [
                  {
                    id: "a5",
                    name: "apple-d",
                    subGroups: [
                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  {
    id: "b2",
    name: "orange",
    subGroups: [
      {
        id: "b2",
        name: "orange-a",
        subGroups: [
          {
            id: "b3",
            name: "orange-b",
            subGroups: [
              {
                id: "b4",
                name: "orange-c",
                subGroups: [
                  {
                    id: "b5",
                    name: "orange-d",
                    subGroups: [
                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
];

如果id = a4,输出应该是:

 [
  {
    id: "a1",
    name: "apple",
    subGroups: [
      {
        id: "a2",
        name: "apple-a",
        subGroups: [
          {
            id: "a3",
            name: "apple-b",
            subGroups: [
              {
                id: "a4",
                name: "apple-c",
                subGroups: [
                  {
                    id: "a5",
                    name: "apple-d",
                    subGroups: [
                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

如果id = b3,输出应该是:

 [
 {
    id: "b2",
    name: "orange",
    subGroups: [
      {
        id: "b2",
        name: "orange-a",
        subGroups: [
          {
            id: "b3",
            name: "orange-b",
            subGroups: [
              {
                id: "b4",
                name: "orange-c",
                subGroups: [
                  {
                    id: "b5",
                    name: "orange-d",
                    subGroups: [
                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

我试过:

const find = (array, id) => (Array.isArray(array) ? 
            array : [array]).flatMap(o => o.id=== id? o : find(o.subGroups, id)), data = group, result = find(data, event.id);

您可以通过 3 个步骤完成:

  1. 使用递归技术来确定每个 child 的 parentID 像这样。
[
  {"id": "a1","parentId": "a1"},
  {"id": "a2","parentId": "a1"},
  ...
  {"id": "b2", "parentId": "b2"},
  {"id": "b3", "parentId": "b2"}
  ...
]
  1. 通过 childID 得到准确的 parentId 像这样
const parentId = child_parent_Mapping.find(r => r.id === childID)?.parentId;
  1. 通过parentId过滤来自arr 的结果。

let arr = [{ id: "a1", name: "apple", subGroups:  [{id: "a2", name: "apple-a", subGroups: [{id: "a3", name: "apple-b", subGroups:  [{id: "a4", name: "apple-c", subGroups: [{id: "a5", name: "apple-d", subGroups:[]}]}]}]}]}, { id: "b2", name: "orange", subGroups:  [{id: "b2", name: "orange-a", subGroups: [{id: "b3", name: "orange-b", subGroups:  [{id: "b4", name: "orange-c", subGroups: [{id: "b5", name: "orange-d", subGroups:[]}]}]}]}]}];

// Step 1
const flatItems = (arr, parentId = "") => {
  return arr.flatMap(({id, subGroups}) => {
    const childrens = flatItems(subGroups, parentId || id);
    return [{id, parentId: parentId || id}, ...childrens];
  });
};
const child_parent_Mapping = flatItems(arr);

const filter_arr = (childID) => {
  // Step 2
  const parentId = child_parent_Mapping.find(r => r.id === childID)?.parentId;
  // Step 3
  return arr.filter(r => r.id === parentId);
} 
console.log({Child: "a1", output: filter_arr('a1')});
console.log({Child: "a4", output: filter_arr('a4')});
console.log({Child: "b3", output: filter_arr('b3')});
.as-console-wrapper { max-height: 100% !important; top: 0; }

我不知道这到底是不是想要的,但是从下面的代码中你可以找到 ultimate 意味着每个 [=11] 的最后 parent =]:

const arr = [{ id: "a1", name: "apple", subGroups:  [{id: "a2", name: "apple-a", subGroups: [{id: "a3", name: "apple-b", subGroups:  [{id: "a4", name: "apple-c", subGroups: [{id: "a5", name: "apple-d", subGroups:[]}]}]}]}]}, { id: "b2", name: "orange", subGroups:  [{id: "b2", name: "orange-a", subGroups: [{id: "b3", name: "orange-b", subGroups:  [{id: "b4", name: "orange-c", subGroups: [{id: "b5", name: "orange-d", subGroups:[]}]}]}]}]}];

const res = [];
const id = "a4";
arr.map(obj=>{
  const str = JSON.stringify(obj);

  if(str.search(id)>-1){
  res.push(obj);
  }
})
console.log(res); //result array