javascript - Get all paths in JSON object -


been messing while , can't right. i'm trying create arrays every path json.

[   {     "webapp : calendar": {       "count": 3151,       "next": {         "viewworkout": {           "count": 521,           "next": {             "beginusersession": {               "count": 12,               "next": {}             },             "editworkout": {               "count": 134,               "next": {}             },             "webapp : expandoopened": {               "count": 116,               "next": {}             },             "mobile : feed": {               "count": 2,               "next": {}             },             "webapp : athleteloadedfromlibrary": {               "count": 45,               "next": {}             },             "viewworkout": {               "count": 108,               "next": {}             },             "webapp : workoutquickviewtabmapgraphclicked": {               "count": 18,               "next": {}             },             "deleteworkout": {               "count": 9,               "next": {}             },             "webapp : headerworkouticonclicked": {               "count": 3,               "next": {}             },             "webapp : notificationcenteropened": {               "count": 14,               "next": {}             },             "webapp : calendar": {               "count": 3,               "next": {}             },             "webapp : workoutquickviewtabsummaryclicked": {               "count": 1,               "next": {}             },             "addworkout": {               "count": 8,               "next": {}             },             "deleteevent": {               "count": 1,               "next": {}             },             "viewworkoutmapandgraph": {               "count": 2,               "next": {}             },             "webapp : fileuploadmenuopened": {               "count": 7,               "next": {}             },             "webapp : athletesettingsloadedfromlibrary": {               "count": 1,               "next": {}             },             "webapp : workoutquickviewtabpowerclicked": {               "count": 2,               "next": {}             },             "webapp : distributionchartloaded": {               "count": 2,               "next": {}             },             "webapp : homeviewed": {               "count": 2,               "next": {}             },             "webapp : loadnotificationfeedbacklocation": {               "count": 3,               "next": {}             },             "mobile : athletechanged": {               "count": 1,               "next": {}             },             "webapp : workoutquickviewtabheartrateclicked": {               "count": 2,               "next": {}             },             "webapp : qvunitsmenutupdateunitstometricclicked": {               "count": 1,               "next": {}             },             "webapp : gotolastweek": {               "count": 4,               "next": {}             },             "webapp : createworkoutfromlibrary": {               "count": 2,               "next": {}             },             "addevent": {               "count": 1,               "next": {}             },             "viewathletelist": {               "count": 1,               "next": {}             },             "webapp : planloadedfromlibrary": {               "count": 2,               "next": {}             },             "webapp : enterfullscreen": {               "count": 1,               "next": {}             },             "webapp : downloadfileclicked": {               "count": 1,               "next": {}             },             "webapp : contextmenuopened": {               "count": 1,               "next": {}             }           }         },         "webapp : athleteloadedfromlibrary": {           "count": 1230,           "next": {             "webapp : calendar": {               "count": 1190,               "next": {}             },             "webapp : gotolastweek": {               "count": 3,               "next": {}             },             "viewworkout": {               "count": 12,               "next": {}             },             "beginusersession": {               "count": 7,               "next": {}             },             "addworkout": {               "count": 3,               "next": {}             },             "webapp : athleteloadedfromlibrary": {               "count": 7,               "next": {}             },             "webapp : refreshcalendar": {               "count": 1,               "next": {}             },             "webapp : quickviewopened": {               "count": 1,               "next": {}             },             "webapp : notificationcenteropened": {               "count": 1,               "next": {}             },             "webapp : selectcalendardate": {               "count": 1,               "next": {}             },             "webapp : gotonextweek": {               "count": 1,               "next": {}             },             "webapp : createworkoutfromlibrary": {               "count": 1,               "next": {}             }           }         },         "webapp : notificationcenteropened": {           "count": 276,           "next": {             "webapp : loadnotificationfeedbacklocation": {               "count": 111,               "next": {}             },             "webapp : athleteloadedfromlibrary": {               "count": 76,               "next": {}             },             "viewworkout": {               "count": 19,               "next": {}             },             "addevent": {               "count": 1,               "next": {}             },             "webapp : notificationcenteropened": {               "count": 9,               "next": {}             },             "addworkout": {               "count": 5,               "next": {}             },             "beginusersession": {               "count": 16,               "next": {}             },             "webapp : calendar": {               "count": 3,               "next": {}             },             "webapp : gotolastweek": {               "count": 2,               "next": {}             },             "webapp : addathlete": {               "count": 5,               "next": {}             },             "$campaign_delivery": {               "count": 1,               "next": {}             },             "mobile : feed": {               "count": 2,               "next": {}             },             "webapp : createworkoutfromlibrary": {               "count": 2,               "next": {}             },             "webapp : homeviewed": {               "count": 1,               "next": {}             },             "webapp : dashboardviewed": {               "count": 2,               "next": {}             },             "webapp : athletesettingsloadedfromlibrary": {               "count": 3,               "next": {}             },             "webapp : calendarloadedvialoaddragndrop": {               "count": 2,               "next": {}             },             "webapp : grouploadedfromlibrary": {               "count": 1,               "next": {}             },             "webapp : workoutquickviewtabpowerclicked": {               "count": 1,               "next": {}             },             "webapp : refreshcalendar": {               "count": 1,               "next": {}             },             "cms : download chart exchange chart": {               "count": 1,               "next": {}             },             "webapp : selectcalendardate": {               "count": 1,               "next": {}             }           }         },         "webapp : planloadedfromlibrary": {           "count": 24,           "next": {             "webapp : calendar": {               "count": 23,               "next": {}             },             "addtrainingplanworkout": {               "count": 1,               "next": {}             }           }         }       }     }   } ] 

the real end goal visualize every path d3, right i'm trying figure out how loop through data , possible paths.

output should , take account may more levels of same structure.

webapp : calendar (3151) -> viewworkout (521) -> beginusersession (12) webapp : calendar (3151) -> viewworkout (521) -> editworkout (134) ... webapp : calendar (3151) -> webapp : athleteloadedfromlibrary (1230) -> webapp : calendar (731) 

i think following snippet wanted.

function getpaths(o, root = "", result = []) {    var ok = object.keys(o);    return ok.reduce((a,k) => { var p = root + k + "(" + o[k].count + ") -> ";                                object.keys(o[k].next).length ? getpaths(o[k].next,p,a)                                                              : a.push(p);                                return a;                              },result);  }  var data = [    {      "webapp : calendar": {        "count": 3151,        "next": {          "viewworkout": {            "count": 521,            "next": {              "beginusersession": {                "count": 12,                "next": {}              },              "editworkout": {                "count": 134,                "next": {}              },              "webapp : expandoopened": {                "count": 116,                "next": {}              },              "mobile : feed": {                "count": 2,                "next": {}              },              "webapp : athleteloadedfromlibrary": {                "count": 45,                "next": {}              },              "viewworkout": {                "count": 108,                "next": {}              },              "webapp : workoutquickviewtabmapgraphclicked": {                "count": 18,                "next": {}              },              "deleteworkout": {                "count": 9,                "next": {}              },              "webapp : headerworkouticonclicked": {                "count": 3,                "next": {}              },              "webapp : notificationcenteropened": {                "count": 14,                "next": {}              },              "webapp : calendar": {                "count": 3,                "next": {}              },              "webapp : workoutquickviewtabsummaryclicked": {                "count": 1,                "next": {}              },              "addworkout": {                "count": 8,                "next": {}              },              "deleteevent": {                "count": 1,                "next": {}              },              "viewworkoutmapandgraph": {                "count": 2,                "next": {}              },              "webapp : fileuploadmenuopened": {                "count": 7,                "next": {}              },              "webapp : athletesettingsloadedfromlibrary": {                "count": 1,                "next": {}              },              "webapp : workoutquickviewtabpowerclicked": {                "count": 2,                "next": {}              },              "webapp : distributionchartloaded": {                "count": 2,                "next": {}              },              "webapp : homeviewed": {                "count": 2,                "next": {}              },              "webapp : loadnotificationfeedbacklocation": {                "count": 3,                "next": {}              },              "mobile : athletechanged": {                "count": 1,                "next": {}              },              "webapp : workoutquickviewtabheartrateclicked": {                "count": 2,                "next": {}              },              "webapp : qvunitsmenutupdateunitstometricclicked": {                "count": 1,                "next": {}              },              "webapp : gotolastweek": {                "count": 4,                "next": {}              },              "webapp : createworkoutfromlibrary": {                "count": 2,                "next": {}              },              "addevent": {                "count": 1,                "next": {}              },              "viewathletelist": {                "count": 1,                "next": {}              },              "webapp : planloadedfromlibrary": {                "count": 2,                "next": {}              },              "webapp : enterfullscreen": {                "count": 1,                "next": {}              },              "webapp : downloadfileclicked": {                "count": 1,                "next": {}              },              "webapp : contextmenuopened": {                "count": 1,                "next": {}              }            }          },          "webapp : athleteloadedfromlibrary": {            "count": 1230,            "next": {              "webapp : calendar": {                "count": 1190,                "next": {}              },              "webapp : gotolastweek": {                "count": 3,                "next": {}              },              "viewworkout": {                "count": 12,                "next": {}              },              "beginusersession": {                "count": 7,                "next": {}              },              "addworkout": {                "count": 3,                "next": {}              },              "webapp : athleteloadedfromlibrary": {                "count": 7,                "next": {}              },              "webapp : refreshcalendar": {                "count": 1,                "next": {}              },              "webapp : quickviewopened": {                "count": 1,                "next": {}              },              "webapp : notificationcenteropened": {                "count": 1,                "next": {}              },              "webapp : selectcalendardate": {                "count": 1,                "next": {}              },              "webapp : gotonextweek": {                "count": 1,                "next": {}              },              "webapp : createworkoutfromlibrary": {                "count": 1,                "next": {}              }            }          },          "webapp : notificationcenteropened": {            "count": 276,            "next": {              "webapp : loadnotificationfeedbacklocation": {                "count": 111,                "next": {}              },              "webapp : athleteloadedfromlibrary": {                "count": 76,                "next": {}              },              "viewworkout": {                "count": 19,                "next": {}              },              "addevent": {                "count": 1,                "next": {}              },              "webapp : notificationcenteropened": {                "count": 9,                "next": {}              },              "addworkout": {                "count": 5,                "next": {}              },              "beginusersession": {                "count": 16,                "next": {}              },              "webapp : calendar": {                "count": 3,                "next": {}              },              "webapp : gotolastweek": {                "count": 2,                "next": {}              },              "webapp : addathlete": {                "count": 5,                "next": {}              },              "$campaign_delivery": {                "count": 1,                "next": {}              },              "mobile : feed": {                "count": 2,                "next": {}              },              "webapp : createworkoutfromlibrary": {                "count": 2,                "next": {}              },              "webapp : homeviewed": {                "count": 1,                "next": {}              },              "webapp : dashboardviewed": {                "count": 2,                "next": {}              },              "webapp : athletesettingsloadedfromlibrary": {                "count": 3,                "next": {}              },              "webapp : calendarloadedvialoaddragndrop": {                "count": 2,                "next": {}              },              "webapp : grouploadedfromlibrary": {                "count": 1,                "next": {}              },              "webapp : workoutquickviewtabpowerclicked": {                "count": 1,                "next": {}              },              "webapp : refreshcalendar": {                "count": 1,                "next": {}              },              "cms : download chart exchange chart": {                "count": 1,                "next": {}              },              "webapp : selectcalendardate": {                "count": 1,                "next": {}              }            }          },          "webapp : planloadedfromlibrary": {            "count": 24,            "next": {              "webapp : calendar": {                "count": 23,                "next": {}              },              "addtrainingplanworkout": {                "count": 1,                "next": {}              }            }          }        }      }    }  ],  paths = getpaths(data[0]);  console.log(paths);

you have call getpaths function root object argument. in case getpaths(data[0]); , result array of paths.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -