Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从父菜单递归查找所有子菜单_Javascript_Recursion - Fatal编程技术网

Javascript 从父菜单递归查找所有子菜单

Javascript 从父菜单递归查找所有子菜单,javascript,recursion,Javascript,Recursion,我的JSON结构如下: [ {"menuId":"1001","depth":"1","parentId":"0"}, {"menuId":"1002","depth":"1","parentId":"0"}, {"menuId":"1003","depth":"2","parentId":"1001"}, {"menuId":"1004","depth":"2","parentId":"1001"}, {"menuId":"1005","depth":"

我的JSON结构如下:

[
    {"menuId":"1001","depth":"1","parentId":"0"},
    {"menuId":"1002","depth":"1","parentId":"0"},
    {"menuId":"1003","depth":"2","parentId":"1001"},
    {"menuId":"1004","depth":"2","parentId":"1001"},
    {"menuId":"1005","depth":"3","parentId":"1003"}, 
    {"menuId":"1006","depth":"3","parentId":"1004"}, 
    {"menuId":"1007","depth":"4","parentId":"1006"}, 
    {"menuId":"1008","depth":"4","parentId":"1006"}, 
    {"menuId":"1009","depth":"5","parentId":"1008"}
]
因此,我需要一个(可能)递归函数,它将查找一个menuId的所有子项,甚至是深层嵌套的子项

让我们假设我想找到孩子('1004')。这将返回以下结果:

['1006', '1007', '1008', '1009']

因为每个菜单都可以引用回1004。无需具体订单。深度可以无限延伸。

您可以像这样使用普通递归

var k=
[{“menuId”:“1001”,“深度”:“1”,“父ID”:“0”},
{“menuId”:“1002”,“深度”:“1”,“父ID”:“0”},
{“menuId”:“1003”,“depth”:“2”,“parentId”:“1001”},
{“menuId”:“1004”,“depth”:“2”,“parentId”:“1001”},
{“menuId”:“1005”,“depth”:“3”,“parentId”:“1003”},
{“menuId”:“1006”,“depth”:“3”,“parentId”:“1004”},
{“menuId”:“1007”,“depth”:“4”,“parentId”:“1006”},
{“menuId”:“1008”,“depth”:“4”,“parentId”:“1006”},
{“menuId”:“1009”,“深度”:“5”,“父ID”:“1008”}]
var-res=[];
var findChildren=函数(id){
k、 forEach(obj=>{
if(obj.parentId==id){
res.push(对象菜单UID);
findChildren(对象菜单ID)
}
})
}
findChildren('1004');

控制台日志(res)
您可以通过检查
parentId
并获取结果集的
menuId
来采取迭代和递归的方法。然后再添加新的子项

函数getChildren(数组,id){ 返回数组.reduce((r,{menuId,parentId})=>{ if(parentId==id){ r、 push(menuId,…getChildren(array,menuId)); } 返回r; }, []); } var数据=[{MENUD:“1001”,深度:“1”,parentId:“0”},{MENUD:“1002”,深度:“1”,parentId:“0”},{MENUD:“1003”,深度:“2”,parentId:“1001”},{MENUD:“1005”,深度:“3”,parentId:“1003”,parentId:“1006”,深度:“3”,parentId:“1004”},{MENUD:“1007”,深度:“4”,parentId:“1006”}、{menuId:“1008”、深度:“4”、父ID:“1006”}、{menuId:“1009”、深度:“5”、父ID:“1008”}、, 结果=getChildren(数据“1004”); console.log(result);您可以使用使用递归函数查找子项的
filter()
方法

ES6中的演示

const findChildrens=(数据、菜单ID、操作器、回调)=>{
让filterArr=data.filter(({parentId})=>parentId==menuId);
if(过滤器平均长度){
//带有过滤数据的Concat数组
oputar=[…oputar,…filterArr.map({menuId}=>menuId)];
//再次搜索下一个节点数据的递归调用
FindChildren(数据,oputar[oputar.length-1],oputar,回调);
}否则{
//如果发现
回调(Oputar);
}
}
const arr=[{“menuId”:“1001”,“depth”:“1”,“parentId”:“0”},{“menuId”:“1002”,“depth”:“1”,“parentId”:“0”},{“menuId”:“1003”,“depth”:“2”,“parentId”:“1004”,“depth”:“2”,“parentId”:“1001”},{“menuId”:“1005”,“depth”:“3”,“parentId”:“3”,“parentId”:“1004”},{“menuId”:“1007”,“parentId”:“1006”{menuId:“1008”,“depth:“4”,“parentId:“1006”},{“menuId:“1009”,“depth:“5”,“parentId:“1008”}];
//为1004调用查找子函数
查找儿童(arr,'1004',[],(res)=>{
console.log('1004'res的结果);
});
//调用查找子函数1001
查找儿童(arr,'1001',[],(res)=>{
console.log('1001'res的结果);
});

.as控制台包装{max height:100%!important;top:0;}
一个简单而简短的选项,包含和:

const data=[{“menuId”:“1001”,“depth”:“1”,“parentId”:“0”},{“menuId”:“1002”,“depth”:“1”,“parentId”:“0”},{“menuId”:“1003”,“depth”:“1001”},{“menuId”:“1004”,“depth”:“2”,“parentId”:“1001”},{“menuId”:“1005”,“depth”:“3”},{“menuId”:“1006”,“depth”:“3”,“parentId”:“1004”},{“menuId”:“1004”,“parentId”:“1006”:“1006”:“},{menuId:“1008”,“depth:“4”,“parentId:“1006”},{menuId:“1009”,“depth:“5”,“parentId:“1008”}];
函数findChildren(id){
const menuIds=data.filter({parentId}=>parentId==id.map({menuId}=>menuId);
返回menuIds.concat(…menuIds.map(findChildren));
}

console.log(findChildren(1004))好主意,你试过什么?从技术上讲,他们不是id 1004的孩子,是吗?