首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这个递归函数会跳过一些数字?

为什么这个递归函数会跳过一些数字?
EN

Stack Overflow用户
提问于 2019-03-22 12:44:38
回答 2查看 117关注 0票数 1

从服务器上,我接收到这个JSON对象。它代表一家公司及其相关部门的组织结构图。

我需要能够选择一家公司,使用公司的ID,我需要传递给相关部门的ID数组。

为此,我创建了这个递归函数。它可以工作,但是跳过3个部门,这些部门位于另一个部门中。

这是JSON文件

代码语言:javascript
复制
{
  "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": []
    }
  ]
}

这是打字稿中的功能

代码语言:javascript
复制
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;
  }

这是将这个函数返回到控制台

代码语言:javascript
复制
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

代码语言:javascript
复制
 this.numbers.forEach(row => {
    console.log(row);
  });
 // 1, 2, 3, 186

它添加CD 1、2、3和186,但跳过4、7和15。

我遗漏了什么?递归是最好的方法吗?有更简单的方法吗?

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-22 14:18:03

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

虽然答案@aonepathan有效,但我将避免使用函数范围之外的变量。

相反,您要做的就是将函数的结果与当前数组连接起来:

代码语言:javascript
复制
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;
}

另一个选项是将数字数组传递给函数调用,这样就可以去掉返回:

代码语言:javascript
复制
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:

代码语言:javascript
复制
let result = new Array();
recursiveFunction(res, result);
doSomething(result);
票数 2
EN

Stack Overflow用户

发布于 2019-03-22 13:18:09

似乎每次函数再次调用时都会重新初始化numbers数组,请考虑将其移出函数之外:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55299918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档