Skip to content

Instantly share code, notes, and snippets.

@Alevale
Created September 11, 2015 18:10
Show Gist options
  • Save Alevale/6b18c935db4ed448b275 to your computer and use it in GitHub Desktop.
Save Alevale/6b18c935db4ed448b275 to your computer and use it in GitHub Desktop.
TreeSearch over a javascript object
console.log('Those are the paths that have your objects as names.')
function callback(res){
console.log(res);
}
function DPSearch(parent, callB, superParentArray, searchedChild){
for(var child in parent){
if(typeof parent[child] === "object"){
superParentArray.push(child);
DPSearch(parent[child] , callB, superParentArray, searchedChild);
}
if(child === searchedChild){
callB(superParentArray);
}
}
}
var asd = {
a: 'asd',
b: 'sdf',
c: 'uio',
d: {
e: 'tre',
f: {
ga: 'tre',
js: {
e: 'tre'
}
}
}
};
DPSearch(asd, callback, [], 'e');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment