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
Advertisement
Answer
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. 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.
- filter the array on the id
- If there is a length then at least one was found
- Return the first one
- Next filter out all the objects with children
- Then create an array (with
.map()
that only includes the children - This will create an array of arrays, so must flatten it
- If there are no children, then
id
was not found- Return null
- 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