Skip to content

Instantly share code, notes, and snippets.

@tintinweb
Created November 5, 2016 17:16
Show Gist options
  • Save tintinweb/52c80ce533a86be939efa0c38baa8428 to your computer and use it in GitHub Desktop.
Save tintinweb/52c80ce533a86be939efa0c38baa8428 to your computer and use it in GitHub Desktop.
javascript - recursively find a specific key or value in any sub object (quick hack)
var seen = [];
var results = [];
function searchObj (obj, query, strparent, level) {
level = level || 1;
if (level % 3==0 || level==1)
console.info(level);
for (var key in obj) {
var orig_sig = strparent + "." + key;
var sig = orig_sig.split(".").slice(-2).join(".");
if (seen.indexOf(sig) >=0){
//console.log("skipping (seen): "+sig);
return;
}else if (orig_sig.indexOf("next")>=0 || orig_sig.indexOf("parent")>=0 || orig_sig.indexOf("child")>=0 || orig_sig.indexOf("sibli")>=0){
return;
}
try{
var value = obj[key];
if (typeof value === 'object') {
searchObj(value, query, orig_sig, level+1);
}
if (value === query || key === query) {
console.warn(orig_sig + ' property=' + key + ' value=' + value);
results.push(orig_sig + "="+ value);
}
} catch(e) {}
seen.push(sig)
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment