Given a data structure as follows:
var endpoints = {
// top level
"orders": {
url: "/orders",
// child
"sub-level": {
url: "/sublevel"
}
},
// users
"users": {
url: "/users",
// child
"another-sublevel": {
url: "/another-sublevel"
}
}
}
How could I recurse over this generating a "Route" object each time I encounter a URL? I also need to track the parents to a route, so:
var Route = function(name, url, parents) {
}
Where name is the key (e.g. "orders" or "users" in the top level) url is obvious and "parents" is some sort of stack that's generated as we drill down each level.
I've had a couple of goes at writing this and I'm running into trouble with variable scope / passing attributes by reference vs. value and all sorts of other weirdness.
The data structure is not fixed either, but needs to contain that information.