动态嵌套对象数组的递归迭代

我使用角度JS和他们的一个例子: http : //jsfiddle.net/furf/EJGHX/

我需要在更新函数发生时获取数据,并在发送到服务器之前为其添加一些值。 (如果使用angular而不是js这样做会更好,让我知道)

我正试图获得’parentid’和’index’并更新孩子们。

这是我正在循环的数据

{ "children": [{ "id": "5", "parentid": "0", "text": "Device Guides", "index": "1", "children": [{ "id": "10", "index": "0", "text": "Grandstream GXP-21XX" }, { "id": "11", "index": "1", "text": "Polycom Soundstation/Soundpoint" }, { "id": "23", "index": "2", "text": "New Polycom" }] }, { "id": "6", "parentid": "0", "text": "Pre-Sales Evaluation", "index": "0", "children": [] }, { "id": "7", "parentid": "0", "text": "Router Setup Guides", "index": "2", "children": [{ "id": "9", "index": "0", "text": "Sonicwall" }, { "id": "12", "index": "1", "text": "Cisco" }] }, { "id": "9", "parentid": "7", "text": "Sonicwall", "index": "0", "children": [] }, { "id": "10", "parentid": "5", "text": "Grandstream GXP-21XX", "index": "0", "children": [] }, { "id": "11", "parentid": "5", "text": "Polycom Soundstation/Soundpoint", "index": "1", "children": [] }, { "id": "12", "parentid": "7", "text": "Cisco", "index": "1", "children": [] }, { "id": "15", "parentid": "0", "text": "Post-Sales Implementation Check List", "index": "7", "children": [{ "id": "16", "index": "0", "text": "Porting and New Number Details" }, { "id": "18", "index": "1", "text": "Partner Setup" }, { "id": "19", "index": "2", "text": "test" }, { "id": "21", "index": "3", "text": "test" }] }, { "id": "16", "parentid": "15", "text": "Porting and New Number Details", "index": "0", "children": [] }, { "id": "18", "parentid": "15", "text": "Partner Setup", "index": "1", "children": [] }, { "id": "19", "parentid": "15", "text": "test", "index": "2", "children": [] }, { "id": "20", "parentid": "0", "text": "test", "index": "11", "children": [] }, { "id": "21", "parentid": "15", "text": "test", "index": "3", "children": [] }, { "id": "23", "parentid": "5", "text": "New Polycom", "index": "2", "children": [] }, { "id": "24", "parentid": "0", "text": "Test Markup", "index": "14", "children": [] }, { "id": "25", "parentid": "0", "text": "test", "index": "15", "children": [] }] } 

这就是我目前正在循环它,但它只获得第一个维度

 for (i = 0, l = data.length; i  0) { for (q = 0, r = data[i].children.length; q < r; q++) { data[i].children[q].parentid = parentid; data[i].children[q].index = q; } } } } 

我发现这个是另一个小提琴,但我不知道如何抓住parentid或索引

 $.each(target.children, function(key, val) { recursiveFunction(key, val) }); function recursiveFunction(key, val) { actualFunction(key, val); var value = val['children']; if (value instanceof Object) { $.each(value, function(key, val) { recursiveFunction(key, val) }); } } function actualFunction(key, val) {} 

如果我正确地理解你,你希望每个’child’都有一个parentID (由其父级定义;否则为0 )和一个index (基于它在兄弟集合中的位置)。

 function normalize(parent) { if (parent && parent.children) { for (var i = 0, l = parent.children.length; i < l; ++i) { var child = parent.children[i]; child.index = i; if (!child.parentId) child.parentId = parent.id || '0'; normalize(child); } } } normalize(data); 

递归是在同一个函数内调用函数。 你的样本根本不是递归;

 function runRecursive(input) { for (var i = 0, l = input.length; i < l; i++) { var current = input[i]; parentid = current.id == null ? '0' : current.id; current.index = i; if (current.children && current.children.length > 0) { runRecursive(current.children); }; }; }; runRecursive(data.children); 

您还应该使用var关键字定义i和l,否则它将位于窗口上下文中,并且递归逻辑将被破坏。 虽然我没有得到什么是parentid变量以及为什么它定义在可见代码之外。