Created
April 1, 2011 00:59
-
-
Save robrighter/897565 to your computer and use it in GitHub Desktop.
Walk a JSON/Javascript tree to grab all values for a given key
This file contains hidden or 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
function traverse(obj,func, parent) { | |
for (i in obj){ | |
func.apply(this,[i,obj[i],parent]); | |
if (obj[i] instanceof Object && !(obj[i] instanceof Array)) { | |
traverse(obj[i],func, i); | |
} | |
} | |
} | |
function getPropertyRecursive(obj, property){ | |
var acc = []; | |
traverse(obj, function(key, value, parent){ | |
if(key === property){ | |
acc.push({parent: parent, value: value}); | |
} | |
}); | |
return acc; | |
} | |
var myobj = { | |
boo :'1', | |
bo1 :'1', | |
bo2 : { | |
boo :'2', | |
test :'2', | |
bo3 : { | |
bar1: '3', | |
bar2: '3', | |
bar3: '3', | |
test: '3', | |
bar4: '3', | |
} | |
}, | |
boo :'1', | |
} | |
console.log(getPropertyRecursive( myobj, 'test' )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment