Skip to content
Advertisement

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’

JavaScript

it is testing array.

JavaScript

I tried a lot of trial, like that. i wanna know. how can i treat maximum call stack ? and i wanna return ‘null’ value.

JavaScript

Advertisement

Answer

This code is the problem:

JavaScript

Two lines above this you initialize result as an array. Then in this conditional test you treat the array result as if it were an object. So, since result.id does not equal id, the else condition recurses for ever and ever.

I’ve taken a different, more functional approach to the task.

  1. filter the array on the 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
  4. This will create an array of arrays, so must flatten it
  5. If there are no children, then id was not found
    • Return null
  6. Recurse the children
    JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement