Skip to content

Instantly share code, notes, and snippets.

@NearlyUnique
Last active August 29, 2015 14:05
Show Gist options
  • Save NearlyUnique/8368ed239047540bf1ef to your computer and use it in GitHub Desktop.
Save NearlyUnique/8368ed239047540bf1ef to your computer and use it in GitHub Desktop.
find({abc:1,def:{ghi:"hi"}}, “ghi”) // finds the full object path to "def.ghi"
/*global console*/
function find(target, searchFor, path) {
"use strict";
try {
path = path || "";
var i, attr, attrs = Object.keys(target);
searchFor = searchFor.toLowerCase();
for (i = 0; i < attrs.length; i += 1) {
attr = attrs[i];
if (attr.toLowerCase().indexOf(searchFor) >= 0) {
console.log((path + '.' + attr).substr(1));
}
if (target[attr] && typeof target[attr] === "object") {
find(target[attr], searchFor, path + "." + attr);
}
}
} catch (e) {
console.log(target, searchFor, path, e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment