【问题标题】:Find parent node from child from child node id?从子节点id中查找子节点的父节点?
【发布时间】:2021-07-16 07:56:08
【问题描述】:

我有一个对象数组,其中有多个子节点,如何从子对象 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);

【问题讨论】:

    标签: javascript arrays flatten flatmap


    【解决方案1】:

    您可以分3步完成:

    1. 这样使用递归技术来确定每个孩子的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. 过滤来自arr 的结果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; }

    【讨论】:

      【解决方案2】:

      我不知道这到底是不是想要的,但是从下面的代码中你可以找到 ultimate 意味着每个 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

      【讨论】:

        猜你喜欢
        • 2016-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多