Skip to content

Instantly share code, notes, and snippets.

@skoon
Forked from creationix/find.js
Created December 27, 2010 23:39
Show Gist options
  • Save skoon/756703 to your computer and use it in GitHub Desktop.
Save skoon/756703 to your computer and use it in GitHub Desktop.
function find(root, obj) {
var seen = [];
function search(root, name, depth) {
if (root === obj) {
console.log(name);
return;
}
if (!depth) { return; }
if (seen.indexOf(root) >= 0) { return; }
if (typeof root !== "object") { return; }
seen.push(root);
try {
Object.getOwnPropertyNames(root).forEach(function (key) {
search(root[key], name + "." + key, depth - 1);
});
} catch (err) {}
}
search (root, "root", 5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment