This question was originally asked here
var obj = {
bat: "bee",
bird: "plane",
bee : {
foo : "bar",
test : {
text: "needle"
}
}
};
var path = findPath (obj, ‘needle’);
would yield
path = “bee.test.text”
findPath = (obj, query) -> | |
for k, v of obj | |
if typeof v is "object" | |
path = findPath v, query | |
return "#{k}.#{path}" if path | |
else if v is query | |
return k |
var findPath; | |
findPath = function(obj, query) { | |
var k, path, v; | |
for (k in obj) { | |
v = obj[k]; | |
if (typeof v === "object") { | |
path = findPath(v, query); | |
if (path) { | |
return k + "." + path; | |
} | |
} else if (v === query) { | |
return k; | |
} | |
} | |
}; |
This question was originally asked here
var obj = {
bat: "bee",
bird: "plane",
bee : {
foo : "bar",
test : {
text: "needle"
}
}
};
var path = findPath (obj, ‘needle’);
would yield
path = “bee.test.text”