Created
October 8, 2015 22:22
-
-
Save coder36/169f197fbb17c18408f0 to your computer and use it in GitHub Desktop.
Search deeply nested javascript object for property
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 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