Tuesday, 15 February 2011

javascript - Why does concurrent flattener returns only 2 inner most children -



javascript - Why does concurrent flattener returns only 2 inner most children -

i can't figure out why tree 'flattener' returning innermost children, expectation should homecoming flattened tree.

var x = { "fields": { "id": "1", "masteraccountid": "", "parentaccountid": "", "name": "name 1" }, "children": [{ "fields": { "id": "2", "masteraccountid": "1", "parentaccountid": "1", "name": "name 2" }, "children": [{ "fields": { "id": "5", "masteraccountid": "1", "parentaccountid": "2", "name": "name 5" }, "children": [{ "fields": { "id": "6", "masteraccountid": "1", "parentaccountid": "5", "name": "name 6" } }, { "fields": { "id": "7", "masteraccountid": "1", "parentaccountid": "5", "name": "name 7" } }] }] }] } function recurs(n) { console.log(n.fields.name); homecoming (n.children != undefined ? $.map(n.children, recurs) : n); } var r = recurs(x);

it returns elements id 6, 7, while console.logs 5 of them.

http://plnkr.co/edit/ldhir86edbnzfah6aalg?p=preview

your function returns n if n.children undefined. since want flat array objects have build one.

function recurs(n) { var out = []; out.push(n.fields); if (n.children) { (var i=0, c; c = n.children[i]; i++) { out.push.apply(out, recurs(c)); } } homecoming out; }

javascript jquery concurrency tree

No comments:

Post a Comment