【问题标题】:How could i fix in Recursion code in javascript我如何在 javascript 中修复递归代码
【发布时间】:2021-02-17 17:27:59
【问题描述】:

我的代码就是这样。起初它是工作..不是更多

function HA7(arr, id) { 
  for (let i = 0; i < arr.length; i++) { 
    if(arr[i]['id']=== id) {
      return arr[i]
    }
      else if(arr[i]=== undefined && arr[i][id] === id  ) {
      return arr[i][id]
    }
      else if(arr[i][id] === undefined){
      return null ;
    }
    return HA7(arr[i],id)
  }
}

我想通过递归函数继续工作.. 但是一旦 agian 只返回 null。 (我想捕获 case id 值为 null 或 arr 未定义)

// example code.

let arr = [
  {
    id: 1,
    name: 'johnny',
  },
  {
    id: 2,
    name: 'ingi',
    children: [
      {
        id: 3,
        name: 'johnson',
      },
      {
        id: 5,
        name: 'steve',
        children: [
          {
            id: 6,
            name: 'lisa',
          },
        ],
      },
      {
        id: 11,
      },
    ],
  },
  {
    id: '13',
  },
];

let output = HA7(arr, 1);
console.log(output); // --> { id: 1, name: 'johnny' }

output = HA7(arr, 5);
console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }

output = HA7(arr, 99);
console.log(output); // --> null

我应该如何修复它?请告诉我一些提示..

【问题讨论】:

    标签: javascript recursion


    【解决方案1】:

    转换 1

    for(;;) 循环替换为for..of -

    function HA7(arr, id) { 
      for (const x of arr) { 
        if(x.id === id) {
          return x
        }
        else if(x=== undefined && x[id] === id  ) {
          return x[id]
        }
        else if(x[id] === undefined){
          return null
        }
        return HA7(x,id)
      }
    }
    

    由于将所有arr[i] 替换为简单的x,因此更容易查看发生了什么。我们还将x["id"] 更改为简单的x.id。现在我们可以看到其他问题了。

    1. 在检查 xnull 还是 undefined 之前,您正在测试 x.idx[id]

    2. 您像x === undefined &amp;&amp; x[id] === id 这样的支票总是为假。这是因为如果 x 未定义,&amp;&amp; x[id] 不仅会为 false,还会抛出错误,因为您正在尝试查找空对象的属性

    3. 你写的是x[id],你可能是想写x.id

    4. 在我们用尽搜索之前,您正在写return null


    转换 2

    让我们解决上面发现的所有问题 -

    function HA7(arr, id) { 
      for (const x of arr) {
        if (x == null)           // <- check nulls first, always
          continue               // <- don't return yet
        else if (x.id == id)          // <- or when id matches
          return x                    // <- return x
        else                                  // <- otherwise
          for (const child of HA7(x.children, id)) // <- search children
            if (result != null)                    // <- if result is found
              return result                        // <- return it
      }
    }
    

    转换 3

    使用生成器我们可以做得更好 -

    function* HA7 (arr, id)
    { for (const x of arr)
      { if (x == null) continue
        if (x.id == id) yield x
        if (x.children) yield *HA7(x.children, id)   
      }
    }
    
    function first (t)
    { for (const x of t)
        return x
    }
    
    const input =
      [{id: 1,name: 'johnny'},{id: 2,name: 'ingi',children: [{id: 3,name: 'johnson'},{id: 5,name: 'steve',children: [{id: 6,name: 'lisa'},],},{id: 11},],},{id: '13'}]
    
    console.log(first(HA7(input, 1)))
    console.log(first(HA7(input, 5)))
    console.log(first(HA7(input, 99)))
    {
      "id": 1,
      "name": "johnny"
    }
    
    {
      "id": 5,
      "name": "steve",
      "children": [
        {
          "id": 6,
          "name": "lisa"
        }
      ]
    }
    
    undefined
    

    我刚刚写了一篇与这个问题非常相似的帖子。请参阅this Q&A 了解使用生成器解决此类问题的其他说明和其他优势。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 2016-03-05
      • 2015-09-10
      • 1970-01-01
      相关资源
      最近更新 更多