[英]Find parent node from child from child node id?
我有一个 object 数组,其中有多个子节点,如何从子 object id 中找到父 object。
[
{
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
,则 output 应该是:
[
{
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
, output 应该是:
[
{
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 个步骤完成:
parentID
。[
{"id": "a1","parentId": "a1"},
{"id": "a2","parentId": "a1"},
...
{"id": "b2", "parentId": "b2"},
{"id": "b3", "parentId": "b2"}
...
]
childID
准确获取parentId
const parentId = child_parent_Mapping.find(r => r.id === childID)?.parentId;
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; }
我不知道这到底是不是想要的,但是从下面的代码中,您可以找到每个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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.