Skip to content

Instantly share code, notes, and snippets.

@coder36
Created October 8, 2015 22:22
Show Gist options
  • Save coder36/169f197fbb17c18408f0 to your computer and use it in GitHub Desktop.
Save coder36/169f197fbb17c18408f0 to your computer and use it in GitHub Desktop.
Search deeply nested javascript object for property
function find_nested_prop( obj, propName, callback ) {
for( var key in obj ) {
var val = obj[key]
if ( key === propName ) {
callback(val, obj)
}
else if ( typeof val === 'object' ) {
find_nested_prop( val, propName, callback )
}
}
}
var json = { aa: { href: 'http1'}, bb: { cc: { href: 'http2' } }, dd: [], ee: [{ff: {href: 'http3'}}] }
find_nested_prop( json, 'href', function( val, parent) {
console.log(parent);
console.log(val);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment