简体   繁体   中英

Find parent node from child from child node id?

I have an array of object which has multiple sub nodes in it, how to find the parent object from child object 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: [
                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
];

if id = a4 , the output should be:

 [
  {
    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: [
                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

if id = b3 , output should be:

 [
 {
    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: [
                      
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

I've tried:

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);

You can complete in 3 steps:

  1. Using recursive technical to determine parentID of each child like this.
[
  {"id": "a1","parentId": "a1"},
  {"id": "a2","parentId": "a1"},
  ...
  {"id": "b2", "parentId": "b2"},
  {"id": "b3", "parentId": "b2"}
  ...
]
  1. Get exactly parentId by childID like this
const parentId = child_parent_Mapping.find(r => r.id === childID)?.parentId;
  1. Filter result from arr by parentId .

 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; }

I don't know this is what exactly wanted or not, but from the below code you can find the ultimate means last parent of every child id :

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM