从服务器上,我接收到这个JSON对象。它代表一家公司及其相关部门的组织结构图。
我需要能够选择一家公司,使用公司的ID,我需要传递给相关部门的ID数组。
为此,我创建了这个递归函数。它可以工作,但是跳过3个部门,这些部门位于另一个部门中。
这是JSON文件
{
"cd": 1,
"cd_base": 0,
"nome": "EMPRESA A",
"children": [
{
"cd": 2,
"cd_base": 1,
"nome": "Departamento A",
"children": [
{
"cd": 4,
"cd_base": 2,
"nome": "Serviço A1",
"children": []
},
{
"cd": 15,
"cd_base": 2,
"nome": "Serviço A2",
"children": []
}
]
},
{
"cd": 3,
"cd_base": 1,
"nome": "Departamento B",
"children": [
{
"cd": 7,
"cd_base": 3,
"nome": "Serviço B1",
"children": []
}
]
},
{
"cd": 186,
"cd_base": 1,
"nome": "Departamento XX",
"children": []
}
]
}这是打字稿中的功能
recursiveFunction(res: any): any[] {
const numbers = new Array(); // to store the ID
console.log('Im on ' + res.cd + ' | ' + res.nome);
numbers.push(res.cd);
if (res.children.length > 0) {
console.log(res.cd + ' | ' + res.nome + ' has children');
res.children.forEach((row) => {
numbers.push(row.cd);
this.recursiveFunction(row);
});
} else {
console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children');
}
return numbers;
}这是将这个函数返回到控制台
Im on 1 | EMPRESA A
1 | EMPRESA A has c
Im on 2 | Departamento A
2 | Departamento A has children
Im on 4 | Serviço A1
4 | Serviço A1 doesn't have any children
Im on 15 | Serviço A2
15 | Serviço A2 doesn't have any children
Im on 3 | Departamento B
3 | Departamento B has children
Im on 7 | Serviço B1
7 | Serviço B1 doesn't have any children
Im on 186 | Departamento XX
186 | Departamento XX doesn't have any children 然后我记录数字数组,结果是1,2,3,186
this.numbers.forEach(row => {
console.log(row);
});
// 1, 2, 3, 186它添加CD 1、2、3和186,但跳过4、7和15。
我遗漏了什么?递归是最好的方法吗?有更简单的方法吗?
任何帮助都是非常感谢的。
发布于 2019-03-22 14:18:03
这是因为您已经定义了一个返回结果的递归函数,但是没有使用该结果。
虽然答案@aonepathan有效,但我将避免使用函数范围之外的变量。
相反,您要做的就是将函数的结果与当前数组连接起来:
recursiveFunction(res: any): any[] {
let numbers = new Array(); // to store the ID
console.log('Im on ' + res.cd + ' | ' + res.nome);
numbers.push(res.cd);
if (res.children.length > 0) {
console.log(res.cd + ' | ' + res.nome + ' has children');
res.children.forEach((row) => {
numbers = numbers.concat(this.recursiveFunction(row));
});
} else {
console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children');
}
return numbers;
}另一个选项是将数字数组传递给函数调用,这样就可以去掉返回:
recursiveFunction(res: any, numbers: any[]) {
console.log('Im on ' + res.cd + ' | ' + res.nome);
numbers.push(res.cd);
if (res.children.length > 0) {
console.log(res.cd + ' | ' + res.nome + ' has children');
res.children.forEach((row) => {
this.recursiveFunction(row, numbers);
});
} else {
console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children');
}
}当您第一次调用它时,将使用一个新的Array:
let result = new Array();
recursiveFunction(res, result);
doSomething(result);发布于 2019-03-22 13:18:09
似乎每次函数再次调用时都会重新初始化numbers数组,请考虑将其移出函数之外:
const numbers = new Array();
function recursiveFunction(res: any): any[] {
console.log('Im on ' + res.cd + ' | ' + res.nome);
numbers.push(res.cd);
if (res.children.length > 0) {
console.log(res.cd + ' | ' + res.nome + ' has children');
res.children.forEach((row) => {
// numbers.push(row.cd);
this.recursiveFunction(row);
});
} else {
console.log(res.cd + ' | ' + res.nome + ' doesn\'t have any children');
}
return numbers;
}我去掉了第二次推送,因为一旦您回忆起这个函数,ID就会被推入数字数组。
控制台: 1,2,4,15,3,7,186
https://stackoverflow.com/questions/55299918
复制相似问题