javascript - flatten and uneven array into an object -


i have array , object representing same data:

arrayexample = [   {name: "max", age: 21},   {name: "max.david", age: 27},   {name: "max.sylvia"},   {name: "max.david.jeff"},   {name: "max.sylvia.anna", age: 20},   {name: "max.david.buffy"},   {name: "max.sylvia.craig"},   {name: "max.sylvia.robin"} ]; objectexample = {   name: "max",   age: 21,   children: [     {       name: "sylvia",       children: [         {name: "craig"},         {name: "robin"},         {name: "anna", age: 20}       ]     },     {       name: "david",       age: 27,       children: [         {name: "jeff"},         {name: "buffy"}       ]     }   ] }; 

my objective extend array class have 2 functions flatten transform objectexample arrayexample , uneven opposite, i'm thinking maybe lodash here still didn't find correct way here's i'm now:
flatten objectexample arrayexample first objectexample structure must specific meaning parents must share property children sure parents , children have other property should ported proper item in new arrayexample, uneven function should create object parents share same property children , other property should copied respectively.
give use case i'm trying make d3js tree layout of angular ui router in application generated routes json file since make routes in json file.

update:

my specific problem need create d3js tree layout angular-ui-router configurations states object can extract json file said before, structure ui-router arrayexample, , required structure d3js tree layout objectexample, 1 way go manually rewrite , wont take time solution not want need make build task generic routes have name attribute in config object used find children of each route or state, more information check ui-router routes config object , d3 videos d3 tre layout:

correction: extending object class flatten function flatten object array , array class uneven function uneven array object not wrote before:

my objective extend array class have 2 functions.

update 2:

to make more clear, both flatten , uneven map function except flatten object not array , return array, , uneven function array return object.

here's function produce flattened output:

working demo: http://jsfiddle.net/jfriend00/w134l7c6/

var objectexample = {   name: "max",   age: 35,   status: "single",   hometown: "scottsdale",   children: [     {       name: "sylvia",       children: [           {name: "craig", age: 16},         {name: "robin"},         {name: "anna"}       ]     },     {       name: "david",       age: 54,       children: [         {name: "jeff"},         {name: "buffy"}       ]     }   ] };  // call on object name property // , optional children property (which array of objects) function flatten(obj, key, outputarray, rootname) {     var name, item;     outputarray = outputarray || [];     rootname = rootname || "";     if (rootname) {         rootname += ".";     }     if (obj.hasownproperty(key)) {         name = rootname + obj[key];         item = {};         item[key] = name;         (var prop in obj) {             if (obj.hasownproperty(prop) && prop !== "children") {                 item[prop] = obj[prop];             }         }         outputarray.push(item)         if (obj.children) {             (var = 0; < obj.children.length; i++) {                 flatten(obj.children[i], key, outputarray, name);             }         }     }     return outputarray; }  var result = flatten(objectexample, "name"); 

produces output:

[{"name":"max","age":35,"status":"single","hometown":"scottsdale"}, {"name":"max.sylvia"}, {"name":"max.sylvia.craig","age":16}, {"name":"max.sylvia.robin"}, {"name":"max.sylvia.anna"}, {"name":"max.david","age":54}, {"name":"max.david.jeff"}, {"name":"max.david.buffy"}] 

you adapt function method on array prototype if want (not recommend, particularly since input isn't array).

i not know mean when "the rootname have more one". objectexample object , cannot have more 1 name @ top level. if started array of objectexample structures, loop on array calling flatten() on each object in top level array , accumulate results.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -