Created
September 11, 2015 18:10
-
-
Save Alevale/6b18c935db4ed448b275 to your computer and use it in GitHub Desktop.
TreeSearch over a javascript object
This file contains 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
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