Created
December 24, 2010 23:49
-
-
Save creationix/754572 to your computer and use it in GitHub Desktop.
Find all references to an object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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