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
x
21
21
1
function ha7(arr, id) { // i wanna find object with id
2
3
let result = [];
4
for(let i = 0 ; i < arr.length ; i++) {
5
if(arr[i].id === id) {
6
return arr[i] // found id, then return included object
7
}
8
else if(Array.isArray(arr[i].children)){ // but , its array
9
// let ar = ha7(arr[i].children, id)
10
result.push(arr[i].children) // i put 'arr[i].children' to variables
11
}
12
}
13
if (result.id === id) {
14
return result // find object with id in inner
15
16
} else {
17
return ha7(result, id) // cant find. then go ahead!
18
}
19
return null // all of none exist id is return null
20
}
21
it is testing array.
JavaScript
1
39
39
1
let input = [
2
{
3
id: 1,
4
name: 'johnny',
5
},
6
{
7
id: 2,
8
name: 'ingi',
9
children: [
10
{
11
id: 3,
12
name: 'johnson',
13
},
14
{
15
id: 5,
16
name: 'steve',
17
children: [
18
{
19
id: 6,
20
name: 'lisa',
21
},
22
],
23
},
24
{
25
id: 11,
26
},
27
],
28
},
29
{
30
id: '13',
31
},
32
];
33
34
output = ha7(input, 5);
35
console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }
36
37
output = ha7(input, 99);
38
console.log(output); // --> null
39
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
1
59
59
1
function ha7(arr, id) { // i wanna find object with id
2
3
let result = [];
4
for(let i = 0 ; i < arr.length ; i++) {
5
if(arr[i].id === id) {
6
return arr[i] // found id, then return included object
7
}
8
else if(Array.isArray(arr[i].children)){ // but , its array
9
// let ar = ha7(arr[i].children, id)
10
result.push(arr[i].children) // i put 'arr[i].children' to variables
11
}
12
}
13
if (result.id === id) {
14
return result // find object with id in inner
15
16
} else {
17
return ha7(result, id) // cant find. then go ahead!
18
}
19
return null // all of none exist id is return null
20
}
21
22
let input = [
23
{
24
id: 1,
25
name: 'johnny',
26
},
27
{
28
id: 2,
29
name: 'ingi',
30
children: [
31
{
32
id: 3,
33
name: 'johnson',
34
},
35
{
36
id: 5,
37
name: 'steve',
38
children: [
39
{
40
id: 6,
41
name: 'lisa',
42
},
43
],
44
},
45
{
46
id: 11,
47
},
48
],
49
},
50
{
51
id: '13',
52
},
53
];
54
55
output = ha7(input, 5);
56
console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }
57
58
output = ha7(input, 99);
59
console.log(output); // --> null
Advertisement
Answer
This code is the problem:
JavaScript
1
7
1
if (result.id === id) {
2
return result // find object with id in inner
3
4
} else {
5
return ha7(result, id) // cant find. then go ahead!
6
}
7
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
JavaScript116161
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"}];
2
3
4function ha7(arr, id) {
5let found = arr.filter(o => o.id === id);
6if (found.length) return found[0]; // return first match
7let children = arr.filter(o=>!!o.children).map(c=>c.children).flat();
8if(!children.length) return null;
9return ha7(children, id);
10}
11
12output = ha7(input, 5);
13console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }
14
15output = ha7(input, 99);
16console.log(output); // --> null