Skip to content

Instantly share code, notes, and snippets.

@dhilipsiva
Last active August 29, 2015 14:27
Show Gist options
  • Save dhilipsiva/4a81b9f9646726d2c03e to your computer and use it in GitHub Desktop.
Save dhilipsiva/4a81b9f9646726d2c03e to your computer and use it in GitHub Desktop.
Find path of a value in an object
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;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment