Typescript-为什么此递归函数跳过一些数字?

时间:2019-03-22 12:44:39

标签: json angular typescript

我从服务器收到了这个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": []
    }
  ]
}

这是Typescript中的函数

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 

然后我记录 numbers 数组,结果为 1,2,3,186

 this.numbers.forEach(row => {
    console.log(row);
  });
 // 1, 2, 3, 186

它将添加CD 1、2、3和186,但跳过4、7和15。 所有这些都是另一个分支/节点内的分支/节点

我想念什么?递归是做到这一点的最佳方法吗?有没有更简单的方法?

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

这是因为您已经定义了一个返回结果的递归函数,但是您没有使用该结果。

虽然@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');
}
}

您第一次调用此命令将是使用新的数组:

let result = new Array();
recursiveFunction(res, result);
doSomething(result);

答案 1 :(得分:1)

似乎每次您再次调用该函数时,您可能都在重新初始化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