Created
February 28, 2018 01:20
-
-
Save greaveselliott/8450eba7cb2313b0d4b143f747751b8c to your computer and use it in GitHub Desktop.
Searches JS object for values
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
function find_property(object, property) { | |
var results = []; | |
for (var key in object) { | |
if (object !== undefined) { | |
var value = object[key]; | |
if (key === property) { | |
results.push({ | |
"property": key, | |
"value": value | |
}); | |
} | |
if (typeof value === 'object') { | |
var inner_result = find_property(value, property); | |
if (inner_result.length > 0 && inner_result !== undefined) { | |
results.push(inner_result[0]); | |
} | |
} | |
} | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment