用于展平嵌套数组并跟踪所有父节点的递归函数(Javascript)
嗨,我有一个函数,它接受包含子对象的父对象嵌套数组并将其展平。但是,我还想跟踪它的所有高级节点ID 数据结构示例:用于展平嵌套数组并跟踪所有父节点的递归函数(Javascript),javascript,algorithm,recursion,Javascript,Algorithm,Recursion,嗨,我有一个函数,它接受包含子对象的父对象嵌套数组并将其展平。但是,我还想跟踪它的所有高级节点ID 数据结构示例: [{ id: 1, type: "group", name: "Colors", items: [ { id: 2, type: "item", name: "Red", items: [] }, { id: 3, type: "item
[{
id: 1,
type: "group",
name: "Colors",
items: [
{ id: 2, type: "item", name: "Red", items: [] },
{ id: 3, type: "item", name: "Purple2", items: [] },
{
id: 4,
type: "item",
name: "Black",
items: [
{
id: 5,
type: "item",
name: "Purple3",
items: [],
},
{ id: 6, type: "item", name: "Purple4", items: [] },
],
},
],
}]
这是当前的最终结果(这正是我想要实现的),但下面的函数可能可以重构
{id: 1, name: "Colors", depth: 1, parentId: null, main: []}
1: {id: 2, name: "Red", depth: 2, parentId: 1, main: [1]}
2: {id: 3, name: "Purple2", depth: 2, parentId: 1, main: [1]}
3: {id: 4, name: "Black", depth: 2, parentId: 1, main: [1]}
4: {id: 5, name: "Purple3", depth: 3, parentId: 4, main: [1,4]} // main stores id of all higher parents e.g. id 4 = "black" and parent of 4 = "Colors"
5: {id: 6, name: "Purple4", depth: 3, parentId: 4, main: [1,4]}
我当前的展平阵列功能:
const flattenArr = (data, depth = 1, parent = null) => {
const result = [];
data.forEach((item) => {
const { id, name, items } = item;
result.push({
id,
name,
depth,
parentId: parent,
main: [],
});
if (items) result.push(...flattenArr(items, depth + 1, item.id));
});
return result;
};
下面的函数用于跟踪所有父ID。然而,这似乎不是我可以使用的最理想的解决方案,因此我想知道它是否可以改进,可以在一个递归函数中完成
const collectParents = (arr) => {
for (let b = 0; b < arr.length; b++) {
if (arr[b].depth !== 1) {
arr[b].main.push(
...arr[b - 1].main,
arr[b - 1].depth === arr[b].depth - 1 ? arr[b - 1].id : null
);
}
}
arr.forEach((x) => (x.main = x.main.filter((i) => i !== null)));
};
const collectParents=(arr)=>{
for(设b=0;b(x.main=x.main.filter((i)=>i!==null));
};
你不能在main
和depth
上使用闭包
const
平面=(主=[],深度=1,)=>({top,type,items=[],…o})=>[
{…o,depth,parentId:main[0]??null,main},
…项目.平面图(平面([…主,o.id],深度+1))
],
tree=[{id:1,type:“group”,name:“Colors”,items:[{id:2,type:“item”,name:“Red”,items:[],top:new Set()},{id:3,type:“item”,name:“Purple2”,items:[]},{id:4,type:“item”,type:“item”,name:“Black”,items:[{id:6,type:“item”,name:“Purple4”,items:[]}]},
结果=tree.flatMap(flat());
控制台日志(结果)代码>
.as console wrapper{max height:100%!important;top:0;}
您不能在main
和depth
上使用闭包
const
平面=(主=[],深度=1,)=>({top,type,items=[],…o})=>[
{…o,depth,parentId:main[0]??null,main},
…项目.平面图(平面([…主,o.id],深度+1))
],
tree=[{id:1,type:“group”,name:“Colors”,items:[{id:2,type:“item”,name:“Red”,items:[],top:new Set()},{id:3,type:“item”,name:“Purple2”,items:[]},{id:4,type:“item”,type:“item”,name:“Black”,items:[{id:6,type:“item”,name:“Purple4”,items:[]}]},
结果=tree.flatMap(flat());
控制台日志(结果)代码>
。作为控制台包装{max height:100%!important;top:0;}
您可以使用reduce
方法创建递归函数,并传递父id和以前的id数组
const data=[{“id”:1,“type”:“group”,“name”:“Colors”,“items”:[{“id”:2,“type”:“item”,“name”:“Red”,“items”:[],“top”:{},{“id”:3,“type”:“item”,“name”:“Purple2”,“items”:[]},{“id”:4,“type”:“item”,“name”:“Black”,“items”:[{“id”:5,“type”:“item”,“item”,“item”,“name”:“item”,“item”,“items”[},{“id”:6,“type”:“item”,“item”,“name”:“Purple4”,“items][}]
函数展平(数据,深度=0,父ID=null,主=[])){
返回数据.reduce((r,{items,id,…rest})=>{
const obj={…rest,id,depth,parentId,main}
r、 推送(obj)
if(项目长度){
r、 推送(…展平(项目,深度+1,id,[…主,id]))
}
返回r;
}, [])
}
常数结果=展平(数据)
log(result)
您可以使用reduce
方法创建递归函数,并传递父id和以前的id数组
const data=[{“id”:1,“type”:“group”,“name”:“Colors”,“items”:[{“id”:2,“type”:“item”,“name”:“Red”,“items”:[],“top”:{},{“id”:3,“type”:“item”,“name”:“Purple2”,“items”:[]},{“id”:4,“type”:“item”,“name”:“Black”,“items”:[{“id”:5,“type”:“item”,“item”,“item”,“name”:“item”,“item”,“items”[},{“id”:6,“type”:“item”,“item”,“name”:“Purple4”,“items][}]
函数展平(数据,深度=0,父ID=null,主=[])){
返回数据.reduce((r,{items,id,…rest})=>{
const obj={…rest,id,depth,parentId,main}
r、 推送(obj)
if(项目长度){
r、 推送(…展平(项目,深度+1,id,[…主,id]))
}
返回r;
}, [])
}
常数结果=展平(数据)
console.log(结果)