In Javascript how can I get the full ancestral hierarchy in a string given just a single object? Right now I can’t even think about how to ask the question… so I can’t even google it. Here’s an example:
var lvl1 = { one: "one", two: "two" };
lvl1.lvl2 = {flip:"flip", flam:"flam"};
lvl1.lvl2.lvl3 = {who:"who", what:"what"};
test(o) {
alert( hierarchyToString(o) );
}
var tmp = lvl1.lvl2.lvl3;
test(tmp);
I want to see:
"lvl1.lvl2.lvl3"
possible? what if I passed in the final leaf string:
test(lvl1.lvl2.lvl3.what);
possible? hopefully that code makes sense… just off top of my head…
Thank you!
Not possible using plain JavaScript objects as there are no
parentreferences. See this question for more details.Unfortunately it’s easy to go from the string representation to the object, but not the other way around. You will have to explicitly code the parent references in each object, and each leaf node to be able to traverse upwards.