algorithm - javascript tree structure - traverse tree structure and show the sum the count attribute inside children -


so have tree stucture below have count attribute in leaf nodes. want sum count , put sum of counts against parent. parent1 , parent2 have sum of children. , continue on there, grantparent has sum of parent1 , parent2. have function traverse tree. getting count unable get.

any ideas?

javascript

 function transverse(element, result, issegmentdata) {         if (element instanceof array){             element.foreach(function (item) {                 { transverse(item, result, issegmentdata); }             });         }         else if (element instanceof object) {             if (element.hasownproperty("count")) {                 // sum count , provide parent             }             if (element.hasownproperty("childnodes")) {                 transverse(element.childnodes, result, issegmentdata);             }         }     } 

tree structure

    [   {     "nodeid": 66318,     "nodename": "grand parent",     "childnodes": [       {         "nodeid": 66323,         "nodename": "parent1",         "childnodes": [           {             "nodeid": 66324,             "nodename": "child1",             "childnodes": [],             "count": 25           },           {             "nodeid": 66334,             "nodename": "child2",             "childnodes": [],             "count": 85           },           {             "nodeid": 66439,             "nodename": "child3",             "childnodes": [],             "count": 65           },           {             "nodeid": 66462,             "nodename": "child4",             "childnodes": [],             "count": 954           }         ]       },       {         "nodeid": 66323,         "nodename": "parent2",         "childnodes": [           {             "nodeid": 66324,             "nodename": "child1",             "childnodes": [],             "count": 225           },           {             "nodeid": 66334,             "nodename": "child2",             "childnodes": [],             "count": 815           }         ]       }     ]   } ] 

you use named function , call arrays again. assign count, if not given count children.

var data = [{ "nodeid": 66318, "nodename": "grand parent", "childnodes": [{ "nodeid": 66323, "nodename": "parent1", "childnodes": [{ "nodeid": 66324, "nodename": "child1", "childnodes": [], "count": 25 }, { "nodeid": 66334, "nodename": "child2", "childnodes": [], "count": 85 }, { "nodeid": 66439, "nodename": "child3", "childnodes": [], "count": 65 }, { "nodeid": 66462, "nodename": "child4", "childnodes": [], "count": 954 }] }, { "nodeid": 66323, "nodename": "parent2", "childnodes": [{ "nodeid": 66324, "nodename": "child1", "childnodes": [], "count": 225 }, { "nodeid": 66334, "nodename": "child2", "childnodes": [], "count": 815 }] }] }];    data.reduce(function x(r, a) {      a.count = a.count || array.isarray(a.childnodes) && a.childnodes.reduce(x, 0) || 0;      return r + a.count;  }, 0);    document.write('<pre>' + json.stringify(data, 0, 4) + '</pre>');  console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -