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. but if not, I want to return ‘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

Leave a Comment