此处是代码:var ob = { name: "root", id: 1, children: [ { name: "child one", id: 11, children: [ { name: "grand child 1", id: 111, children: [] }, { name: "grand child 2", id: 112, children: [] } ]}, { name: "child two", id: 12, children: [] } ]};function findObjectById(root, id) { if (root.children) { for (var k in root.children) { if (root.children[k].id == id) { return root.children[k]; } else if (root.children.length) { return findObjectById(root.children[k], id); } } } };function findObjectByLabel(obj, id) { for (i in obj.children) { if(obj.children[i].id === id) { document.write( obj.children[i].id); l= obj.children[i].children.length +1 return obj.children[i]; } else { findObjectByLabel( obj.children[i], id); } }};function traverse(o,spc) { for (i in o) { if(i == "name"|| i == "id") { document.write(spc + i + " : "+o[i]+"<br>"); } if (typeof(o[i])=="object") { //going on step down in the object tree!! ss = spc+"_" traverse(o[i],ss); } }}var s =""traverse(ob,s)var boo = findObjectByLabel(ob,111)var l = boo.children.lengthdocument.write(typeof(boo))document.write("<br>"+"boo kids l= " +l+"<br>")//var boo = findObjectByLabel(ob,111)boo.children.push({ name: "child x", id: 11111, children: []});var ll = boo.children.lengthdocument.write("<br>"+"boo kids l= " +ll+"<br>")var s =""traverse(ob,s)解决方案我创建了该函数,该函数可以实现我想要的功能:function testpush(o,idd,tt,tid) { for(var a = 0; a < o.children.length; a++) { if(o.children[a].id == idd) { o.children[a].children.push({ name: tt, id: tid, children: [] }); } else { testp(o.children[a],idd,tt,tid); } }};testp(ob,12,"test 121",121)testp(ob,121,"test 1211",1211)I have tried to use a code snippet from this SO question:How to add an object to a nested javascript object using a parent idas you try to insert further down the tree it does nit work as undefined is returned.I've tried to solve this but I''m missing somethingso i need some help please.http://jsfiddle.net/murray_3/EbJ85/4/heres the code:var ob = { name: "root", id: 1, children: [ { name: "child one", id: 11, children: [ { name: "grand child 1", id: 111, children: [] }, { name: "grand child 2", id: 112, children: [] } ]}, { name: "child two", id: 12, children: [] } ]};function findObjectById(root, id) { if (root.children) { for (var k in root.children) { if (root.children[k].id == id) { return root.children[k]; } else if (root.children.length) { return findObjectById(root.children[k], id); } } } };function findObjectByLabel(obj, id) { for (i in obj.children) { if(obj.children[i].id === id) { document.write( obj.children[i].id); l= obj.children[i].children.length +1 return obj.children[i]; } else { findObjectByLabel( obj.children[i], id); } }};function traverse(o,spc) { for (i in o) { if(i == "name"|| i == "id") { document.write(spc + i + " : "+o[i]+"<br>"); } if (typeof(o[i])=="object") { //going on step down in the object tree!! ss = spc+"_" traverse(o[i],ss); } }}var s =""traverse(ob,s)var boo = findObjectByLabel(ob,111)var l = boo.children.lengthdocument.write(typeof(boo))document.write("<br>"+"boo kids l= " +l+"<br>")//var boo = findObjectByLabel(ob,111)boo.children.push({ name: "child x", id: 11111, children: []});var ll = boo.children.lengthdocument.write("<br>"+"boo kids l= " +ll+"<br>")var s =""traverse(ob,s) 解决方案 I created this function which does what i wanted:function testpush(o,idd,tt,tid) { for(var a = 0; a < o.children.length; a++) { if(o.children[a].id == idd) { o.children[a].children.push({ name: tt, id: tid, children: [] }); } else { testp(o.children[a],idd,tt,tid); } }};testp(ob,12,"test 121",121)testp(ob,121,"test 1211",1211) 这篇关于javascript推入嵌套树对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 04:07