简体   繁体   English

嵌套对象返回未定义的递归函数

[英]recursive function for nested object returning undefined

I'm looping through a nested object of objects, looking for a specific object and if I'm I find it, I do stuff. 我正在遍历对象的嵌套对象,寻找一个特定的对象,如果我找到它,我就会做。 I can get it working for the first nest, but any nest after that I get an undefined value. 我可以将其用于第一个嵌套,但是此后的任何嵌套都将得到未定义的值。

 let myObj = [{ id: 1, children: [{ id: 1.1, children: [] }, { id: 1.2, children: [] } ] }, { id: 2, children: [{ id: 2.1, children: [] }, { id: 2.2, children: [] } ] } ] function addToObj(itemToAdd, parentId, obj) { for (let i = 0; i < obj.length; i++) { const item = search(obj[i], parentId); console.log(item); // undefined if (item) { item.children = item.children.concat(itemToAdd); break; } } function search(obj, id) { if (obj.id === id) { console.log(obj); // defined (obj with id of 2.1), but returns undefined? return obj; } for (let i = 0; i < obj.children.length; i++) { search(obj.children[i], id); } } return obj; }; const itemToAdd = { id: 100, } addToObj(itemToAdd, 2.1, myObj); 

The function in the above snippet loops through the object, looking for a specific item. 上面的代码片段中的函数遍历对象,寻找特定项目。 If it finds the item it will insert an object into that items children property. 如果找到该项目,它将在该项目的子项属性中插入一个对象。

You need to use the return value from the recursive search : if it exists, return it: 您需要使用递归search的返回值:如果存在,则返回它:

for (let i = 0; i < obj.children.length; i++) {
  const possibleResult = search(obj.children[i], id);
  if (possibleResult) {
    return possibleResult;
  }
}

 let myObj = [{ id: 1, children: [{ id: 1.1, children: [] }, { id: 1.2, children: [] } ] }, { id: 2, children: [{ id: 2.1, children: [] }, { id: 2.2, children: [] } ] } ] function addToObj(itemsToAdd, parentId, obj) { for (let i = 0; i < obj.length; i++) { const item = search(obj[i], parentId); // first log here will be undefined, nothing found // second log here will find the object console.log('item', item); if (item) { item.children = item.children.concat(itemsToAdd); break; } } function search(obj, id) { if (obj.id === id) { console.log('obj', obj); // defined (obj with id of 2.1), but returns undefined? return obj; } for (let i = 0; i < obj.children.length; i++) { const possibleResult = search(obj.children[i], id); if (possibleResult) { return possibleResult; } } } return obj; }; const itemToAdd = { id: 100, } addToObj(itemToAdd, 2.1, myObj); 

There are two problems in code 代码中有两个问题

  • if (obj.id === id) is false then in loop you are returning nothing. 如果(obj.id === id)false则循环中不返回任何内容。
  • You should check if obj.children exists before loop. 您应该在循环之前检查obj.children存在。

 let myObj = [ { id: 1, children: [ { id: 1.1, children: [] }, { id: 1.2, children: [] } ] }, { id: 2, children: [ { id: 2.1, children: [] }, { id: 2.2, children: [] } ] } ] There are two problems in code: - List item function addToObj(itemToAdd, parentId, obj) { for (let i=0;i<obj.length;i++) { const item = search(obj[i], parentId); console.log(item); // undefined if (item) { item.children = item.children.concat(itemToAdd); break; } } function search(obj, id) { if (obj.id === id) { console.log(obj); // defined (obj with id of 2.1), but returns undefined? return obj; } if(obj.children){ for (let i=0;i<obj.children.length;i++) { let x = search(obj.children[i], id); if(x) return x; } } } return obj; }; const itemToAdd = { id: 100, } addToObj(itemToAdd, 2.1, myObj); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM