Skip to content

Instantly share code, notes, and snippets.

@creationix
Created December 24, 2010 23:49
Show Gist options
  • Select an option

  • Save creationix/754572 to your computer and use it in GitHub Desktop.

Select an option

Save creationix/754572 to your computer and use it in GitHub Desktop.

Revisions

  1. creationix created this gist Dec 24, 2010.
    19 changes: 19 additions & 0 deletions find.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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);
    }