简体   繁体   English

我需要帮助如何处理最大调用堆栈?

[英]i need a help how can i treat maximum call stack?

Thanks i fixed some sentence by advice.谢谢我通过建议修正了一些句子。 my code is like that,我的代码是这样的,

i wanna find object with id.我想找到带有 id 的 object。 but if not, I want to return 'null'但如果不是,我想返回'null'

    function ha7(arr, id) {  // i wanna find object with id 

    let result = [];
    for(let i = 0 ; i < arr.length ; i++) {
      if(arr[i].id === id) {   
        return arr[i]   // found id, then return included object
      }
    else if(Array.isArray(arr[i].children)){ // but , its array
       // let ar = ha7(arr[i].children, id)
        result.push(...arr[i].children)      // i put 'arr[i].children' to variables 
      } 
    }
      if (result.id === id) {
        return result                      //  find object with id in inner
 
      } else {
        return ha7(result, id)             // cant find. then go ahead! 
      }
    return null                            // all of none exist id is return null
    }

it is testing array.它正在测试数组。

    let 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',
      },
    ];

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

    output = ha7(input, 99);
    console.log(output); // --> null

I tried a lot of trial, like that.我试了很多次,就是这样。 i wanna know.我想知道。 how can i treat maximum call stack?我如何处理最大调用堆栈? and i wanna return 'null' value.我想返回“空”值。

 function ha7(arr, id) { // i wanna find object with id let result = []; for(let i = 0; i < arr.length; i++) { if(arr[i].id === id) { return arr[i] // found id, then return included object } else if(Array.isArray(arr[i].children)){ // but, its array // let ar = ha7(arr[i].children, id) result.push(...arr[i].children) // i put 'arr[i].children' to variables } } if (result.id === id) { return result // find object with id in inner } else { return ha7(result, id) // cant find. then go ahead: } return null // all of none exist id is return null } let 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', }; ], output = ha7(input; 5). console;log(output): // --> { id, 5: name, 'steve': children: [{ id, 6: name, 'lisa' }] } output = ha7(input; 99). console;log(output); // --> null

This code is the problem:这段代码是问题所在:

   if (result.id === id) {
        return result                      //  find object with id in inner
 
      } else {
        return ha7(result, id)             // cant find. then go ahead! 
      }

Two lines above this you initialize result as an array.在这上面的两行你将result初始化为一个数组。 Then in this conditional test you treat the array result as if it were an object.然后在此条件测试中,您将数组result视为 object。 So, since result.id does not equal id , the else condition recurses for ever and ever.因此,由于result.id不等于id ,否则 else 条件将永远递归。

I've taken a different, more functional approach to the task.我对这项任务采取了一种不同的、更实用的方法。

  1. filter the array on the id过滤id上的数组
    • If there is a length then at least one was found如果有长度,则至少找到一个
    • Return the first one返回第一个
  2. Next filter out all the objects with children接下来过滤掉所有有孩子的对象
  3. Then create an array (with .map() that only includes the children然后创建一个数组(使用.map()仅包含子项
  4. This will create an array of arrays, so must flatten it这将创建一个 arrays 数组,因此必须将其展平
  5. If there are no children, then id was not found如果没有孩子,则没有找到id
    • Return null返回 null
  6. Recurse the children递归孩子
     let 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"}]; function ha7(arr, id) { let found = arr.filter(o => o.id === id); if (found.length) return found[0]; // return first match let children = arr.filter(o=>..o.children).map(c=>c;children).flat(); if(,children;length) return null, return ha7(children; id). } output = ha7(input; 5): console,log(output): // --> { id, 5: name: 'steve', children: [{ id, 6; name. 'lisa' }] } output = ha7(input; 99); console.log(output); // --> null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复'超出最大调用堆栈大小'AngularJS - How can I fix 'Maximum call stack size exceeded' AngularJS 最大调用堆栈大小超出错误…如何解决? - maximum call stack size exceeded error… how do I fix? 我正在堆栈:超出最大调用堆栈大小的错误 - i am getting stack : Maximum call stack size exceeded error 需要帮助找到未捕获的RangeError的来源:超出了最大调用堆栈大小 - Need help finding the source of an Uncaught RangeError: Maximum call stack size exceeded 我有这个错误: RangeError: 超出最大调用堆栈大小 - I have this error : RangeError: Maximum call stack size exceeded 为什么我会收到“RangeError:超出最大调用堆栈大小”? - Why do i get “RangeError: Maximum call stack size exceeded”? 为什么我会超出最大调用堆栈数? - Why do i get a maximum call stack exceeded? 如何修复此 memory 泄漏监视器功能以避免最大调用堆栈循环? - How do I fix this memory leak monitor func to avoid maximum call stack loop? 我该如何纠正这个错误? 超出最大调用堆栈大小错误 - How do i remedy this error? Maximum Call Stack Size Exceeded Error 我想从 openweathermap 加载数据,以便我可以显示它,但我得到 Uncaught RangeError: Maximum call stack size exceeded - i want to load the data from openweathermap so i can display it but am getting Uncaught RangeError: Maximum call stack size exceeded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM