[英]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.我对这项任务采取了一种不同的、更实用的方法。
.map()
that only includes the children.map()
仅包含子项id
was not foundid
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.